blob: fd469e68e755dade60ae9e16084acc91c614d53d [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
sfricke-samsungef15e482022-01-26 11:32:49 -0800313SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &module_state) {
314 for (auto insn : module_state) {
sfricke-samsung5a48ed42022-02-13 17:37:13 -0800315 const uint32_t result_word = OpcodeResultWord(insn.opcode());
316 if (result_word != 0) {
317 def_index[insn.word(result_word)] = insn.offset();
318 }
319
sfricke-samsung962cad92021-04-13 00:46:29 -0700320 switch (insn.opcode()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700321 // Specialization constants
322 case spv::OpSpecConstantTrue:
323 case spv::OpSpecConstantFalse:
324 case spv::OpSpecConstant:
325 case spv::OpSpecConstantComposite:
326 case spv::OpSpecConstantOp:
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600327 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700328 break;
329
sfricke-samsung962cad92021-04-13 00:46:29 -0700330 // Decorations
331 case spv::OpDecorate: {
332 auto target_id = insn.word(1);
333 decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
334 decoration_inst.push_back(insn);
335 if (insn.word(2) == spv::DecorationBuiltIn) {
336 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3)));
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600337 } else if (insn.word(2) == spv::DecorationSpecId) {
338 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700339 }
340
341 } break;
342 case spv::OpGroupDecorate: {
343 auto const &src = decorations[insn.word(1)];
344 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600345 has_group_decoration = true;
346 } break;
347 case spv::OpDecorationGroup:
348 case spv::OpGroupMemberDecorate: {
349 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700350 } break;
351 case spv::OpMemberDecorate: {
352 member_decoration_inst.push_back(insn);
353 if (insn.word(3) == spv::DecorationBuiltIn) {
354 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4)));
355 }
356 } break;
357
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600358 // Execution Mode
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800359 case spv::OpExecutionMode:
360 case spv::OpExecutionModeId: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600361 execution_mode_inst[insn.word(1)].push_back(insn);
362 } break;
363
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600364 default:
365 if (AtomicOperation(insn.opcode()) == true) {
366 // All atomics have a pointer referenced
367 spirv_inst_iter access;
368 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800369 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600370 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800371 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600372 }
373
374 atomic_instruction atomic;
375
sfricke-samsungef15e482022-01-26 11:32:49 -0800376 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600377 // spirv-val should catch if not pointer
378 assert(pointer.opcode() == spv::OpTypePointer);
379 atomic.storage_class = pointer.word(2);
380
sfricke-samsungef15e482022-01-26 11:32:49 -0800381 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600382 atomic.type = data_type.opcode();
383
384 // TODO - Should have a proper GetBitWidth like spirv-val does
385 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
386 atomic.bit_width = data_type.word(2);
387
388 atomic_inst[insn.offset()] = atomic;
389 }
390 // We don't care about any other defs for now.
391 break;
392 }
393 }
394
sfricke-samsungef15e482022-01-26 11:32:49 -0800395 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600396 multiple_entry_points = entry_points.size() > 1;
397}
398
399// static
400std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800401 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600402 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
403 function_set func_set = {};
404 EntryPoint *entry_point = nullptr;
405
sfricke-samsungef15e482022-01-26 11:32:49 -0800406 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600407 // offset is not 0, it means it's updated and the offset is in a Function.
408 if (func_set.offset) {
409 func_set.op_lists.emplace(insn.opcode(), insn.offset());
410 } else if (entry_point) {
411 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
412 }
413
414 switch (insn.opcode()) {
415 // Functions
416 case spv::OpFunction:
417 func_set.id = insn.word(2);
418 func_set.offset = insn.offset();
419 func_set.op_lists.clear();
420 break;
421
422 // Entry points ... add to the entrypoint table
423 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700424 // Entry points do not have an id (the id is the function id) and thus need their own table
425 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
426 auto execution_model = insn.word(1);
427 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
428 entry_points.emplace(entrypoint_name,
429 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
430
431 auto range = entry_points.equal_range(entrypoint_name);
432 for (auto it = range.first; it != range.second; ++it) {
433 if (it->second.offset == insn.offset()) {
434 entry_point = &(it->second);
435 break;
436 }
437 }
438 assert(entry_point != nullptr);
439 break;
440 }
441 case spv::OpFunctionEnd: {
442 assert(entry_point != nullptr);
443 func_set.length = insn.offset() - func_set.offset;
444 entry_point->function_set_list.emplace_back(func_set);
445 break;
446 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700447 }
448 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600449
sfricke-samsungef15e482022-01-26 11:32:49 -0800450 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600451 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700452}
453
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600454void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
455 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700456 spvtools::Optimizer optimizer(env);
457 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
458 std::vector<uint32_t> optimized_binary;
459 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600460 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
461
sfricke-samsung962cad92021-04-13 00:46:29 -0700462 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600463 // NOTE: We need to update words with the result from the spirv-tools optimizer.
464 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
465 // of the SHADER_MODULE_STATE instance.
466 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700467 }
468 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700469}
470
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800471char const *StorageClassName(uint32_t sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700472 switch (sc) {
473 case spv::StorageClassInput:
474 return "input";
475 case spv::StorageClassOutput:
476 return "output";
477 case spv::StorageClassUniformConstant:
478 return "const uniform";
479 case spv::StorageClassUniform:
480 return "uniform";
481 case spv::StorageClassWorkgroup:
482 return "workgroup local";
483 case spv::StorageClassCrossWorkgroup:
484 return "workgroup global";
485 case spv::StorageClassPrivate:
486 return "private global";
487 case spv::StorageClassFunction:
488 return "function";
489 case spv::StorageClassGeneric:
490 return "generic";
491 case spv::StorageClassAtomicCounter:
492 return "atomic counter";
493 case spv::StorageClassImage:
494 return "image";
495 case spv::StorageClassPushConstant:
496 return "push constant";
497 case spv::StorageClassStorageBuffer:
498 return "storage buffer";
499 default:
500 return "unknown";
501 }
502}
503
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800504void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700505 auto insn = get_def(type);
506 assert(insn != end());
507
508 switch (insn.opcode()) {
509 case spv::OpTypeBool:
510 ss << "bool";
511 break;
512 case spv::OpTypeInt:
513 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
514 break;
515 case spv::OpTypeFloat:
516 ss << "float" << insn.word(2);
517 break;
518 case spv::OpTypeVector:
519 ss << "vec" << insn.word(3) << " of ";
520 DescribeTypeInner(ss, insn.word(2));
521 break;
522 case spv::OpTypeMatrix:
523 ss << "mat" << insn.word(3) << " of ";
524 DescribeTypeInner(ss, insn.word(2));
525 break;
526 case spv::OpTypeArray:
527 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
528 DescribeTypeInner(ss, insn.word(2));
529 break;
530 case spv::OpTypeRuntimeArray:
531 ss << "runtime arr[] of ";
532 DescribeTypeInner(ss, insn.word(2));
533 break;
534 case spv::OpTypePointer:
535 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
536 DescribeTypeInner(ss, insn.word(3));
537 break;
538 case spv::OpTypeStruct: {
539 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800540 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700541 DescribeTypeInner(ss, insn.word(i));
542 if (i == insn.len() - 1) {
543 ss << ")";
544 } else {
545 ss << ", ";
546 }
547 }
548 break;
549 }
550 case spv::OpTypeSampler:
551 ss << "sampler";
552 break;
553 case spv::OpTypeSampledImage:
554 ss << "sampler+";
555 DescribeTypeInner(ss, insn.word(2));
556 break;
557 case spv::OpTypeImage:
558 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
559 break;
560 case spv::OpTypeAccelerationStructureNV:
561 ss << "accelerationStruture";
562 break;
563 default:
564 ss << "oddtype";
565 break;
566 }
567}
568
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800569std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700570 std::ostringstream ss;
571 DescribeTypeInner(ss, type);
572 return ss.str();
573}
574
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800575std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
576 std::ostringstream ss;
577 const uint32_t opcode = insn.opcode();
578 uint32_t operand_offset = 1; // where to start printing operands
579 // common disassembled for SPIR-V is
580 // %result = Opcode %result_type %operands
581 if (OpcodeHasResult(opcode)) {
582 operand_offset++;
583 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
584 }
585
586 ss << string_SpvOpcode(opcode);
587
588 if (OpcodeHasType(opcode)) {
589 operand_offset++;
590 ss << " %" << insn.word(1);
591 }
592
sfricke-samsunged00aa42022-01-27 19:03:01 -0800593 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
594 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
595 // simplest way to print the instruction and give the developer something to look into when an error occurs.
596 //
597 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
598 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800599 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800600 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800601 }
602 return ss.str();
603}
604
sfricke-samsung962cad92021-04-13 00:46:29 -0700605const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
606 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600607 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700608 for (auto it = range.first; it != range.second; ++it) {
609 if (it->second.stage == stageBits) {
610 return &(it->second);
611 }
612 }
613 return nullptr;
614}
615
616spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600617 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700618 for (auto it = range.first; it != range.second; ++it) {
619 if (it->second.stage == stageBits) {
620 return at(it->second.offset);
621 }
622 }
623 return end();
624}
625
626// Because the following is legal, need the entry point
627// OpEntryPoint GLCompute %main "name_a"
628// OpEntryPoint GLCompute %main "name_b"
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800629// Assumes shader module contains no spec constants used to set the local size values
sfricke-samsung962cad92021-04-13 00:46:29 -0700630bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
631 uint32_t &local_size_z) const {
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800632 // "If an object is decorated with the WorkgroupSize decoration, this takes precedence over any LocalSize or LocalSizeId
633 // execution mode."
634 for (const auto &builtin : static_data_.builtin_decoration_list) {
635 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
636 const uint32_t workgroup_size_id = at(builtin.offset).word(1);
637 auto composite_def = get_def(workgroup_size_id);
638 if (composite_def.opcode() == spv::OpConstantComposite) {
639 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32
640 local_size_x = GetConstantValue(get_def(composite_def.word(3)));
641 local_size_y = GetConstantValue(get_def(composite_def.word(4)));
642 local_size_z = GetConstantValue(get_def(composite_def.word(5)));
sfricke-samsung962cad92021-04-13 00:46:29 -0700643 return true;
644 }
645 }
646 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800647
648 auto entrypoint_id = entrypoint.word(2);
649 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
650 if (it != static_data_.execution_mode_inst.end()) {
651 for (auto insn : it->second) {
652 if (insn.opcode() == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeLocalSize) {
653 local_size_x = insn.word(3);
654 local_size_y = insn.word(4);
655 local_size_z = insn.word(5);
656 return true;
657 } else if (insn.opcode() == spv::OpExecutionModeId && insn.word(2) == spv::ExecutionModeLocalSizeId) {
658 local_size_x = GetConstantValueById(insn.word(3));
659 local_size_y = GetConstantValueById(insn.word(4));
660 local_size_z = GetConstantValueById(insn.word(5));
661 return true;
662 }
663 }
664 }
665 return false; // not found
sfricke-samsung962cad92021-04-13 00:46:29 -0700666}
667
668// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
669// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800670spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700671 auto value = get_def(id);
672
673 // If id is a copy, see where it was copied from
674 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
675 id = value.word(3);
676 value = get_def(id);
677 }
678
679 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
680 return value;
681 }
682 return end();
683}
684
685// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800686uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700687 auto value = GetConstantDef(id);
688
689 if (end() == value) {
690 // TODO: Either ensure that the specialization transform is already performed on a module we're
691 // considering here, OR -- specialize on the fly now.
692 return 1;
693 }
694 return GetConstantValue(value);
695}
696
697// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
698// negative.
699int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
700 auto type = get_def(resource.type_id);
701 while (true) {
702 switch (type.opcode()) {
703 case spv::OpTypeSampledImage:
704 type = get_def(type.word(2));
705 break;
706 case spv::OpTypePointer:
707 type = get_def(type.word(3));
708 break;
709 case spv::OpTypeImage:
710 return type.word(3);
711 default:
712 return -1;
713 }
714 }
715}
716
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800717uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700718 auto insn = get_def(type);
719 assert(insn != end());
720
721 switch (insn.opcode()) {
722 case spv::OpTypePointer:
723 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
724 // pointers around.
725 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
726 case spv::OpTypeArray:
727 if (strip_array_level) {
728 return GetLocationsConsumedByType(insn.word(2), false);
729 } else {
730 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
731 }
732 case spv::OpTypeMatrix:
733 // Num locations is the dimension * element size
734 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
735 case spv::OpTypeVector: {
736 auto scalar_type = get_def(insn.word(2));
737 auto bit_width =
738 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
739
740 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
741 return (bit_width * insn.word(3) + 127) / 128;
742 }
743 default:
744 // Everything else is just 1.
745 return 1;
746
747 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
748 }
749}
750
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800751uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700752 auto insn = get_def(type);
753 assert(insn != end());
754
755 switch (insn.opcode()) {
756 case spv::OpTypePointer:
757 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
758 // pointers around.
759 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
760 case spv::OpTypeStruct: {
761 uint32_t sum = 0;
762 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
763 sum += GetComponentsConsumedByType(insn.word(i), false);
764 }
765 return sum;
766 }
767 case spv::OpTypeArray:
768 if (strip_array_level) {
769 return GetComponentsConsumedByType(insn.word(2), false);
770 } else {
771 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
772 }
773 case spv::OpTypeMatrix:
774 // Num locations is the dimension * element size
775 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
776 case spv::OpTypeVector: {
777 auto scalar_type = get_def(insn.word(2));
778 auto bit_width =
779 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
780 // One component is 32-bit
781 return (bit_width * insn.word(3) + 31) / 32;
782 }
783 case spv::OpTypeFloat: {
784 auto bit_width = insn.word(2);
785 return (bit_width + 31) / 32;
786 }
787 case spv::OpTypeInt: {
788 auto bit_width = insn.word(2);
789 return (bit_width + 31) / 32;
790 }
791 case spv::OpConstant:
792 return GetComponentsConsumedByType(insn.word(1), false);
793 default:
794 return 0;
795 }
796}
797
798// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
799// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800800uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700801 auto insn = get_def(type);
802 assert(insn != end());
803
804 switch (insn.opcode()) {
805 case spv::OpTypeInt:
806 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
807 case spv::OpTypeFloat:
808 return FORMAT_TYPE_FLOAT;
809 case spv::OpTypeVector:
810 case spv::OpTypeMatrix:
811 case spv::OpTypeArray:
812 case spv::OpTypeRuntimeArray:
813 case spv::OpTypeImage:
814 return GetFundamentalType(insn.word(2));
815 case spv::OpTypePointer:
816 return GetFundamentalType(insn.word(3));
817
818 default:
819 return 0;
820 }
821}
822
823spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
824 while (true) {
825 if (def.opcode() == spv::OpTypePointer) {
826 def = get_def(def.word(3));
827 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
828 def = get_def(def.word(2));
829 is_array_of_verts = false;
830 } else if (def.opcode() == spv::OpTypeStruct) {
831 return def;
832 } else {
833 return end();
834 }
835 }
836}
837
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800838void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700839 shader_struct_member &data) const {
840 const auto struct_it = GetStructType(it, false);
841 assert(struct_it != end());
842 data.size = 0;
843
844 shader_struct_member data1;
845 uint32_t i = 2;
846 uint32_t local_offset = 0;
847 std::vector<uint32_t> offsets;
848 offsets.resize(struct_it.len() - i);
849
850 // 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 -0800851 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700852 const auto member_decorate = at(offset);
853 if (member_decorate.word(1) != struct_it.word(1)) {
854 continue;
855 }
856
857 offsets[member_decorate.word(2)] = member_decorate.word(4);
858 }
859
860 for (const auto offset : offsets) {
861 local_offset = offset;
862 data1 = {};
863 data1.root = data.root;
864 data1.offset = local_offset;
865 auto def_member = get_def(struct_it.word(i));
866
867 // Array could be multi-dimensional
868 while (def_member.opcode() == spv::OpTypeArray) {
869 const auto len_id = def_member.word(3);
870 const auto def_len = get_def(len_id);
871 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
872 def_member = get_def(def_member.word(2));
873 }
874
875 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800876 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700877 } else if (def_member.opcode() == spv::OpTypePointer) {
878 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
879 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
880 // that "points to something."
881 data1.size = 8;
882 } else {
883 // 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 -0800884 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700885 }
886 } else {
887 if (def_member.opcode() == spv::OpTypeMatrix) {
888 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
889 def_member = get_def(def_member.word(2));
890 }
891
892 if (def_member.opcode() == spv::OpTypeVector) {
893 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
894 def_member = get_def(def_member.word(2));
895 }
896
897 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
898 data1.size = (def_member.word(2) / 8);
899 }
900 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
901 if (array_length_hierarchy_szie > 0) {
902 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
903
904 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
905 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
906 }
907 }
908 data.struct_members.emplace_back(data1);
909 ++i;
910 }
911 uint32_t total_array_length = 1;
912 for (const auto length : data1.array_length_hierarchy) {
913 total_array_length *= length;
914 }
915 data.size = local_offset + data1.size * total_array_length;
916}
917
918static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
919 int array_indices_size = static_cast<int>(array_indices.size());
920 if (array_indices_size) {
921 uint32_t array_index = 0;
922 uint32_t i = 0;
923 for (const auto index : array_indices) {
924 array_index += (data.array_block_size[i] * index);
925 ++i;
926 }
927 offset += (array_index * data.size);
928 }
929 return offset;
930}
931
932static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
933 int array_indices_size = static_cast<int>(array_indices.size());
934 uint32_t block_memory_size = data.size;
935 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
936 block_memory_size *= data.array_length_hierarchy[i];
937 }
938
939 offset = UpdateOffset(offset, array_indices, data);
940
941 uint32_t end = offset + block_memory_size;
942 auto used_bytes = data.GetUsedbytes();
943 if (used_bytes->size() < end) {
944 used_bytes->resize(end, 0);
945 }
946 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
947}
948
949void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
950 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
951 if (access_chain_word_index < access_chain_it.len()) {
952 if (data.array_length_hierarchy.size() > array_indices.size()) {
953 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
954 ++access_chain_word_index;
955
956 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
957 array_indices.emplace_back(def_it.word(3));
958 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
959 } else {
960 // If it is a variable, set the all array is used.
961 if (access_chain_word_index < access_chain_it.len()) {
962 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
963 for (uint32_t i = 0; i < array_length; ++i) {
964 auto array_indices2 = array_indices;
965 array_indices2.emplace_back(i);
966 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
967 }
968 } else {
969 SetUsedBytes(offset, array_indices, data);
970 }
971 }
972 } else {
973 offset = UpdateOffset(offset, array_indices, data);
974 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
975 }
976 } else {
977 SetUsedBytes(offset, array_indices, data);
978 }
979}
980
981void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
982 const shader_struct_member &data) const {
983 std::vector<uint32_t> array_indices_emptry;
984
985 if (access_chain_word_index < access_chain_it.len()) {
986 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
987 ++access_chain_word_index;
988
989 auto data1 = data.struct_members[strcut_member_index];
990 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
991 }
992}
993
994void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
995 const shader_struct_member &data) const {
996 for (const auto &func_set : function_set_list) {
997 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
998 for (auto it = range.first; it != range.second; ++it) {
999 auto access_chain = at(it->second);
1000 if (access_chain.word(3) == variable_id) {
1001 RunUsedStruct(0, 4, access_chain, data);
1002 }
1003 }
1004 }
1005}
1006
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001007// static
1008void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -08001009 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001010 for (auto &entrypoint : entry_points) {
1011 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1012 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001013 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001014
1015 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001016 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001017 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1018 std::vector<uint32_t> offsets;
1019
1020 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001021 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001022 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1023 offsets.emplace_back(member_decorate.offset());
1024 }
1025 }
1026 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001027 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1028 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1029 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001030 }
1031 }
1032 }
1033}
1034
1035uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1036 auto type = get_def(type_id);
1037
1038 while (true) {
1039 switch (type.opcode()) {
1040 case spv::OpTypeArray:
1041 case spv::OpTypeRuntimeArray:
1042 case spv::OpTypeSampledImage:
1043 type = get_def(type.word(2));
1044 break;
1045 case spv::OpTypePointer:
1046 type = get_def(type.word(3));
1047 break;
1048 case spv::OpTypeImage: {
1049 auto dim = type.word(3);
1050 auto arrayed = type.word(5);
1051 auto msaa = type.word(6);
1052
1053 uint32_t bits = 0;
1054 switch (GetFundamentalType(type.word(2))) {
1055 case FORMAT_TYPE_FLOAT:
1056 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1057 break;
1058 case FORMAT_TYPE_UINT:
1059 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1060 break;
1061 case FORMAT_TYPE_SINT:
1062 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1063 break;
1064 default:
1065 break;
1066 }
1067
1068 switch (dim) {
1069 case spv::Dim1D:
1070 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1071 return bits;
1072 case spv::Dim2D:
1073 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1074 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1075 return bits;
1076 case spv::Dim3D:
1077 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1078 return bits;
1079 case spv::DimCube:
1080 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1081 return bits;
1082 case spv::DimSubpassData:
1083 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1084 return bits;
1085 default: // buffer, etc.
1086 return bits;
1087 }
1088 }
1089 default:
1090 return 0;
1091 }
1092 }
1093}
1094
1095// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1096// This function examines instructions in the static call tree for a write to this variable.
1097bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1098 auto type = builtin_instr.opcode();
1099 uint32_t target_id = builtin_instr.word(1);
1100 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001101 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001102
1103 if (type == spv::OpMemberDecorate) {
1104 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1105 auto insn = entrypoint;
1106 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1107 switch (insn.opcode()) {
1108 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001109 if (insn.word(2) == spv::StorageClassOutput) {
1110 const auto type_id = insn.word(3);
1111 if (type_id == target_id) {
1112 target_id = insn.word(1);
1113 } else {
1114 // If the output is an array, check if the element type is what we're looking for
1115 const auto type_insn = get_def(type_id);
1116 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1117 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001118 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001119 }
1120 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001121 }
1122 break;
1123 case spv::OpVariable:
1124 if (insn.word(1) == target_id) {
1125 target_id = insn.word(2);
1126 init_complete = true;
1127 }
1128 break;
1129 }
1130 insn++;
1131 }
1132 }
1133
1134 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1135
1136 bool found_write = false;
1137 layer_data::unordered_set<uint32_t> worklist;
1138 worklist.insert(entrypoint.word(2));
1139
1140 // Follow instructions in call graph looking for writes to target
1141 while (!worklist.empty() && !found_write) {
1142 auto id_iter = worklist.begin();
1143 auto id = *id_iter;
1144 worklist.erase(id_iter);
1145
1146 auto insn = get_def(id);
1147 if (insn == end()) {
1148 continue;
1149 }
1150
1151 if (insn.opcode() == spv::OpFunction) {
1152 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001153 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001154 switch (insn.opcode()) {
1155 case spv::OpAccessChain:
1156 if (insn.word(3) == target_id) {
1157 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001158 // Get the target member of the struct
1159 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1160 // arrays of structs of structs) is not currently supported.
1161 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1162 if (value_itr != end()) {
1163 auto value = GetConstantValue(value_itr);
1164 if (value == builtin_instr.word(2)) {
1165 target_id = insn.word(2);
1166 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001167 }
1168 } else {
1169 target_id = insn.word(2);
1170 }
1171 }
1172 break;
1173 case spv::OpStore:
1174 if (insn.word(1) == target_id) {
1175 found_write = true;
1176 }
1177 break;
1178 case spv::OpFunctionCall:
1179 worklist.insert(insn.word(3));
1180 break;
1181 }
1182 }
1183 }
1184 }
1185 return found_write;
1186}
1187
1188// Used by the collection functions to help aid in state tracking
1189struct shader_module_used_operators {
1190 bool updated;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001191 std::vector<uint32_t> image_read_members;
1192 std::vector<uint32_t> image_write_members;
1193 std::vector<uint32_t> atomic_members;
1194 std::vector<uint32_t> store_members;
1195 std::vector<uint32_t> atomic_store_members;
1196 std::vector<uint32_t> sampler_implicitLod_dref_proj_members; // sampler Load id
1197 std::vector<uint32_t> sampler_bias_offset_members; // sampler Load id
1198 std::vector<uint32_t> image_dref_members;
1199 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_members; // <image,sampler> Load id
1200 layer_data::unordered_map<uint32_t, uint32_t> load_members;
1201 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members;
1202 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001203
1204 shader_module_used_operators() : updated(false) {}
1205
1206 bool CheckImageOperandsBiasOffset(uint32_t type) {
1207 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1208 spv::ImageOperandsConstOffsetsMask)
1209 ? true
1210 : false;
1211 }
1212
sfricke-samsungef15e482022-01-26 11:32:49 -08001213 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001214 if (updated) return;
1215 updated = true;
1216
sfricke-samsungef15e482022-01-26 11:32:49 -08001217 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001218 switch (insn.opcode()) {
1219 case spv::OpImageSampleImplicitLod:
1220 case spv::OpImageSampleProjImplicitLod:
1221 case spv::OpImageSampleProjExplicitLod:
1222 case spv::OpImageSparseSampleImplicitLod:
1223 case spv::OpImageSparseSampleProjImplicitLod:
1224 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001225 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001226 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001227 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1228 sampler_implicitLod_dref_proj_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001229 // ImageOperands in index: 5
1230 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001231 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001232 }
1233 break;
1234 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001235 case spv::OpImageDrefGather:
1236 case spv::OpImageSparseDrefGather: {
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(3) : insn.word(3);
1240 image_dref_members.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001241 break;
1242 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001243 case spv::OpImageSampleDrefImplicitLod:
1244 case spv::OpImageSampleDrefExplicitLod:
1245 case spv::OpImageSampleProjDrefImplicitLod:
1246 case spv::OpImageSampleProjDrefExplicitLod:
1247 case spv::OpImageSparseSampleDrefImplicitLod:
1248 case spv::OpImageSparseSampleDrefExplicitLod:
1249 case spv::OpImageSparseSampleProjDrefImplicitLod:
1250 case spv::OpImageSparseSampleProjDrefExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001251 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001252 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001253 auto sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1254 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1255
1256 image_dref_members.emplace_back(image_load_id);
1257 sampler_implicitLod_dref_proj_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001258 // ImageOperands in index: 6
1259 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001260 sampler_bias_offset_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001261 }
1262 break;
1263 }
1264 case spv::OpImageSampleExplicitLod:
1265 case spv::OpImageSparseSampleExplicitLod: {
1266 // ImageOperands in index: 5
1267 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001268 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001269 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001270 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1271 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001272 }
1273 break;
1274 }
1275 case spv::OpStore: {
1276 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1277 break;
1278 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001279 case spv::OpImageRead:
1280 case spv::OpImageSparseRead: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001281 image_read_members.emplace_back(insn.word(3)); // Load id
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001282 break;
1283 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001284 case spv::OpImageWrite: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001285 image_write_members.emplace_back(insn.word(1)); // Load id
sfricke-samsung962cad92021-04-13 00:46:29 -07001286 break;
1287 }
1288 case spv::OpSampledImage: {
1289 // 3: image load id, 4: sampler load id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001290 sampled_image_members.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001291 break;
1292 }
1293 case spv::OpLoad: {
1294 // 2: Load id, 3: object id or AccessChain id
1295 load_members.emplace(insn.word(2), insn.word(3));
1296 break;
1297 }
1298 case spv::OpAccessChain: {
1299 if (insn.len() == 4) {
1300 // If it is for struct, the length is only 4.
1301 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001302 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001303 } else {
1304 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001305 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 -07001306 }
1307 break;
1308 }
1309 case spv::OpImageTexelPointer: {
1310 // 2: ImageTexelPointer id, 3: object id
1311 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1312 break;
1313 }
1314 default: {
1315 if (AtomicOperation(insn.opcode())) {
1316 if (insn.opcode() == spv::OpAtomicStore) {
1317 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1318 } else {
1319 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1320 }
1321 }
1322 break;
1323 }
1324 }
1325 }
1326 }
1327};
1328
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001329static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1330 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1331 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001332 for (auto load_id : operator_members) {
1333 if (object_id == load_id) return true;
1334 auto load_it = load_members.find(load_id);
1335 if (load_it == load_members.end()) {
1336 continue;
1337 }
1338 if (load_it->second == object_id) {
1339 return true;
1340 }
1341
1342 auto accesschain_it = accesschain_members.find(load_it->second);
1343 if (accesschain_it == accesschain_members.end()) {
1344 continue;
1345 }
1346 if (accesschain_it->second.first == object_id) {
1347 return true;
1348 }
1349 }
1350 return false;
1351}
1352
1353// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1354// atomic operation, matching images to samplers, etc
1355void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1356 interface_var &out_interface_var,
1357 shader_module_used_operators &used_operators) const {
1358 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001359 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001360
1361 auto type = get_def(type_id);
1362
1363 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1364 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1365 type.opcode() == spv::OpTypeSampledImage) {
1366 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1367 type.opcode() == spv::OpTypeSampledImage) {
1368 type = get_def(type.word(2)); // Element type
1369 } else {
1370 type = get_def(type.word(3)); // Pointer type
1371 }
1372 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001373
sfricke-samsung962cad92021-04-13 00:46:29 -07001374 switch (type.opcode()) {
1375 case spv::OpTypeImage: {
1376 auto dim = type.word(3);
1377 if (dim != spv::DimSubpassData) {
1378 used_operators.update(this);
1379
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001380 // Sampled == 2 indicates used without a sampler (a storage image)
1381 bool is_image_without_format = false;
1382 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1383
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001384 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_members, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001385 used_operators.accesschain_members)) {
1386 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001387 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1388 }
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001389 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_members, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001390 used_operators.accesschain_members)) {
1391 out_interface_var.is_readable = true;
1392 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001393 }
1394 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1395 used_operators.accesschain_members)) {
1396 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1397 }
1398 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1399 used_operators.accesschain_members)) {
1400 out_interface_var.is_sampler_bias_offset = true;
1401 }
1402 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1403 used_operators.accesschain_members) ||
1404 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1405 used_operators.accesschain_members)) {
1406 out_interface_var.is_atomic_operation = true;
1407 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001408 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_members, used_operators.load_members,
1409 used_operators.accesschain_members)) {
1410 out_interface_var.is_dref_operation = true;
1411 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001412
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001413 for (auto &itp_id : used_operators.sampled_image_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001414 // Find if image id match.
1415 uint32_t image_index = 0;
1416 auto load_it = used_operators.load_members.find(itp_id.first);
1417 if (load_it == used_operators.load_members.end()) {
1418 continue;
1419 } else {
1420 if (load_it->second != id) {
1421 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1422 if (accesschain_it == used_operators.accesschain_members.end()) {
1423 continue;
1424 } else {
1425 if (accesschain_it->second.first != id) {
1426 continue;
1427 }
1428
1429 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1430 if (const_itr == end()) {
1431 // access chain index not a constant, skip.
1432 break;
1433 }
1434 image_index = GetConstantValue(const_itr);
1435 }
1436 }
1437 }
1438 // Find sampler's set binding.
1439 load_it = used_operators.load_members.find(itp_id.second);
1440 if (load_it == used_operators.load_members.end()) {
1441 continue;
1442 } else {
1443 uint32_t sampler_id = load_it->second;
1444 uint32_t sampler_index = 0;
1445 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1446
1447 if (accesschain_it != used_operators.accesschain_members.end()) {
1448 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1449 if (const_itr == end()) {
1450 // access chain index representing sampler index is not a constant, skip.
1451 break;
1452 }
1453 sampler_id = const_itr.offset();
1454 sampler_index = GetConstantValue(const_itr);
1455 }
1456 auto sampler_dec = get_decorations(sampler_id);
1457 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1458 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1459 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001460
1461 // Need to check again for these properties in case not using a combined image sampler
1462 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_members,
1463 used_operators.load_members, used_operators.accesschain_members)) {
1464 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1465 }
1466 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_members,
1467 used_operators.load_members, used_operators.accesschain_members)) {
1468 out_interface_var.is_sampler_bias_offset = true;
1469 }
1470
sfricke-samsung962cad92021-04-13 00:46:29 -07001471 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001472 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001473 }
1474 }
1475 }
1476 return;
1477 }
1478
1479 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001480 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001481 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001482 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001483 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1484 nonwritable_members.insert(insn.word(2));
1485 }
1486 }
1487
1488 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1489 // as nonwritable.
1490 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1491 used_operators.update(this);
1492
1493 for (auto oid : used_operators.store_members) {
1494 if (id == oid) {
1495 out_interface_var.is_writable = true;
1496 return;
1497 }
1498 auto accesschain_it = used_operators.accesschain_members.find(oid);
1499 if (accesschain_it == used_operators.accesschain_members.end()) {
1500 continue;
1501 }
1502 if (accesschain_it->second.first == id) {
1503 out_interface_var.is_writable = true;
1504 return;
1505 }
1506 }
1507 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1508 used_operators.accesschain_members)) {
1509 out_interface_var.is_writable = true;
1510 return;
1511 }
1512 }
1513 }
1514 }
1515}
1516
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001517std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001518 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001519 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001520 shader_module_used_operators operators;
1521
1522 for (auto id : accessible_ids) {
1523 auto insn = get_def(id);
1524 assert(insn != end());
1525
1526 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001527 (insn.word(3) == spv::StorageClassUniform ||
1528 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001529 insn.word(3) == spv::StorageClassStorageBuffer)) {
1530 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001531 uint32_t set = d.descriptor_set;
1532 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001533
1534 interface_var v = {};
1535 v.id = insn.word(2);
1536 v.type_id = insn.word(1);
1537
1538 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1539 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001540 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001541 }
1542 }
1543
1544 return out;
1545}
1546
1547layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001548 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001549 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001550 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001551 layer_data::unordered_set<uint32_t> store_members;
1552 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001553
1554 for (auto insn : *this) {
1555 switch (insn.opcode()) {
1556 case spv::OpStore:
1557 case spv::OpAtomicStore: {
1558 store_members.insert(insn.word(1)); // object id or AccessChain id
1559 break;
1560 }
1561 case spv::OpAccessChain: {
1562 // 2: AccessChain id, 3: object id
1563 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1564 break;
1565 }
1566 default:
1567 break;
1568 }
1569 }
1570 if (store_members.empty()) {
1571 return location_list;
1572 }
1573 for (auto output : outputs) {
1574 auto store_it = store_members.find(output.second.id);
1575 if (store_it != store_members.end()) {
1576 location_list.insert(output.first.first);
1577 store_members.erase(store_it);
1578 continue;
1579 }
1580 store_it = store_members.begin();
1581 while (store_it != store_members.end()) {
1582 auto accesschain_it = accesschain_members.find(*store_it);
1583 if (accesschain_it == accesschain_members.end()) {
1584 ++store_it;
1585 continue;
1586 }
1587 if (accesschain_it->second == output.second.id) {
1588 location_list.insert(output.first.first);
1589 store_members.erase(store_it);
1590 accesschain_members.erase(accesschain_it);
1591 break;
1592 }
1593 ++store_it;
1594 }
1595 }
1596 return location_list;
1597}
1598
1599bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001600 uint32_t id, uint32_t type_id, bool is_patch,
1601 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001602 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1603 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1604 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1605 // This isn't an interface block.
1606 return false;
1607 }
1608
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001609 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1610 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1611 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001612
1613 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001614 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001615 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001616 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001617
1618 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001619 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001620 member_components[member_index] = component;
1621 }
1622
1623 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1624 member_relaxed_precision[member_index] = 1;
1625 }
1626
1627 if (insn.word(3) == spv::DecorationPatch) {
1628 member_patch[member_index] = 1;
1629 }
1630 }
1631 }
1632
1633 // TODO: correctly handle location assignment from outside
1634
1635 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001636 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001637 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001638 uint32_t member_index = insn.word(2);
1639 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001640
1641 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001642 uint32_t location = insn.word(4);
1643 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001644 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001645 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001646 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1647 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1648
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001649 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001650 interface_var v = {};
1651 v.id = id;
1652 // TODO: member index in interface_var too?
1653 v.type_id = member_type_id;
1654 v.offset = offset;
1655 v.is_patch = member_is_patch;
1656 v.is_block_member = true;
1657 v.is_relaxed_precision = is_relaxed_precision;
1658 (*out)[std::make_pair(location + offset, component)] = v;
1659 }
1660 }
1661 }
1662 }
1663
1664 return true;
1665}
1666
1667std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1668 spv::StorageClass sinterface,
1669 bool is_array_of_verts) const {
1670 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1671
1672 std::map<location_t, interface_var> out;
1673
1674 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1675 auto insn = get_def(iid);
1676 assert(insn != end());
1677 assert(insn.opcode() == spv::OpVariable);
1678
ziga-lunarg9e94e112021-09-27 00:21:10 +02001679 const auto d = get_decorations(iid);
1680 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1681 (d.flags & decoration_set::passthrough_bit) != 0;
1682 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001683 uint32_t id = insn.word(2);
1684 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001685
ziga-lunarg9e94e112021-09-27 00:21:10 +02001686 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001687 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001688 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001689 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1690 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001691 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001692
1693 if (builtin != -1) {
1694 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001695 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1696 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001697 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1698 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001699 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1700 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001701 interface_var v = {};
1702 v.id = id;
1703 v.type_id = type;
1704 v.offset = offset;
1705 v.is_patch = is_patch;
1706 v.is_relaxed_precision = is_relaxed_precision;
1707 out[std::make_pair(location + offset, component)] = v;
1708 }
1709 }
1710 }
1711 }
1712
1713 return out;
1714}
1715
1716std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001717 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001718 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001719 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1720 auto def = get_def(id);
1721 assert(def != end());
1722 assert(def.opcode() == spv::OpVariable);
1723
1724 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1725 }
1726
1727 // Find all members belonging to the builtin block selected
1728 std::vector<uint32_t> builtin_block_members;
1729 for (auto &var : variables) {
1730 auto def = get_def(get_def(var).word(3));
1731
1732 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1733 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1734
1735 // Now find all members belonging to the struct defining the IO block
1736 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001737 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001738 auto insn = at(set.offset);
1739 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1740 // Start with undefined builtin for each struct member.
1741 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1742 if (builtin_block_members.size() == 0) {
1743 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001744 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001745 auto struct_index = insn.word(2);
1746 assert(struct_index < builtin_block_members.size());
1747 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001748 }
1749 }
1750 }
1751 }
1752
1753 return builtin_block_members;
1754}
1755
1756std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1757 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1758 std::vector<std::pair<uint32_t, interface_var>> out;
1759
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001760 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001761 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1762 auto attachment_index = insn.word(3);
1763 auto id = insn.word(1);
1764
1765 if (accessible_ids.count(id)) {
1766 auto def = get_def(id);
1767 assert(def != end());
1768 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1769 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001770 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001771 interface_var v = {};
1772 v.id = id;
1773 v.type_id = def.word(1);
1774 v.offset = offset;
1775 out.emplace_back(attachment_index + offset, v);
1776 }
1777 }
1778 }
1779 }
1780 }
1781
1782 return out;
1783}
1784
ziga-lunarg8346fe82021-08-22 17:30:50 +02001785uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1786 const uint32_t opcode = iter.opcode();
1787 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1788 return 1;
1789 } else if (opcode == spv::OpTypeVector) {
1790 const uint32_t component_count = iter.word(3);
1791 return component_count;
1792 } else if (opcode == spv::OpTypeMatrix) {
1793 const auto column_type = get_def(iter.word(2));
1794 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
ziga-lunarg38e44982022-04-05 00:10:46 +02001795 // Because we are calculating components for a single location we do not care about column count
1796 return vector_length;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001797 } else if (opcode == spv::OpTypeArray) {
1798 const auto element_type = get_def(iter.word(2));
1799 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1800 return element_length;
1801 } else if (opcode == spv::OpTypeStruct) {
1802 uint32_t total_size = 0;
1803 for (uint32_t i = 2; i < iter.len(); ++i) {
1804 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1805 }
1806 return total_size;
1807 } else if (opcode == spv::OpTypePointer) {
1808 const auto type = get_def(iter.word(3));
1809 return GetNumComponentsInBaseType(type);
1810 }
1811 return 0;
1812}
1813
ziga-lunarga26b3602021-08-08 15:53:00 +02001814uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1815 const uint32_t opcode = iter.opcode();
1816 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1817 return iter.word(2);
1818 } else if (opcode == spv::OpTypeVector) {
1819 const auto component_type = get_def(iter.word(2));
1820 uint32_t scalar_width = GetTypeBitsSize(component_type);
1821 uint32_t component_count = iter.word(3);
1822 return scalar_width * component_count;
1823 } else if (opcode == spv::OpTypeMatrix) {
1824 const auto column_type = get_def(iter.word(2));
1825 uint32_t vector_width = GetTypeBitsSize(column_type);
1826 uint32_t column_count = iter.word(3);
1827 return vector_width * column_count;
1828 } else if (opcode == spv::OpTypeArray) {
1829 const auto element_type = get_def(iter.word(2));
1830 uint32_t element_width = GetTypeBitsSize(element_type);
1831 const auto length_type = get_def(iter.word(3));
1832 uint32_t length = GetConstantValue(length_type);
1833 return element_width * length;
1834 } else if (opcode == spv::OpTypeStruct) {
1835 uint32_t total_size = 0;
1836 for (uint32_t i = 2; i < iter.len(); ++i) {
1837 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1838 }
1839 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001840 } else if (opcode == spv::OpTypePointer) {
1841 const auto type = get_def(iter.word(3));
1842 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001843 } else if (opcode == spv::OpVariable) {
1844 const auto type = get_def(iter.word(1));
1845 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001846 }
1847 return 0;
1848}
1849
1850uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1851
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001852// 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 +02001853uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1854 const uint32_t opcode = iter.opcode();
1855 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1856 return iter.word(1);
1857 } else if (opcode == spv::OpTypeVector) {
1858 const auto& component_type = get_def(iter.word(2));
1859 return GetBaseType(component_type);
1860 } else if (opcode == spv::OpTypeMatrix) {
1861 const auto& column_type = get_def(iter.word(2));
1862 return GetBaseType(column_type);
1863 } else if (opcode == spv::OpTypeArray) {
1864 const auto& element_type = get_def(iter.word(2));
1865 return GetBaseType(element_type);
1866 } else if (opcode == spv::OpTypePointer) {
1867 const auto& type = get_def(iter.word(3));
1868 return GetBaseType(type);
1869 }
1870 return 0;
1871}
1872
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001873// Returns type_id if id has type or zero otherwise
1874uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1875 const auto type = get_def(id);
1876 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1877}
1878
ziga-lunarga26b3602021-08-08 15:53:00 +02001879uint32_t SHADER_MODULE_STATE::CalcComputeSharedMemory(VkShaderStageFlagBits stage,
1880 const spirv_inst_iter &insn) const {
1881 if (stage == VK_SHADER_STAGE_COMPUTE_BIT && insn.opcode() == spv::OpVariable) {
1882 uint32_t storage_class = insn.word(3);
1883 if (storage_class == spv::StorageClassWorkgroup) { // StorageClass Workgroup is shared memory
1884 uint32_t result_type_id = insn.word(1);
1885 auto result_type = get_def(result_type_id);
1886 auto type = get_def(result_type.word(3));
1887 return GetTypeBytesSize(type);
1888 }
1889 }
1890
1891 return 0;
1892}
1893
sfricke-samsung962cad92021-04-13 00:46:29 -07001894// Assumes itr points to an OpConstant instruction
1895uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); }
1896
1897std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1898 assert(entrypoint.opcode() == spv::OpEntryPoint);
1899
1900 std::vector<uint32_t> interfaces;
1901 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1902 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1903 uint32_t word = 3;
1904 while (entrypoint.word(word) & 0xff000000u) {
1905 ++word;
1906 }
1907 ++word;
1908
1909 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1910
1911 return interfaces;
1912}