blob: 64ba0e17e140b595c2c9d3b41a6bdfe1b542934c [file] [log] [blame]
sfricke-samsung962cad92021-04-13 00:46:29 -07001/* Copyright (c) 2021 The Khronos Group Inc.
2 *
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
120static unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) {
121 switch (mode) {
122 case spv::ExecutionModelVertex:
123 return VK_SHADER_STAGE_VERTEX_BIT;
124 case spv::ExecutionModelTessellationControl:
125 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
126 case spv::ExecutionModelTessellationEvaluation:
127 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
128 case spv::ExecutionModelGeometry:
129 return VK_SHADER_STAGE_GEOMETRY_BIT;
130 case spv::ExecutionModelFragment:
131 return VK_SHADER_STAGE_FRAGMENT_BIT;
132 case spv::ExecutionModelGLCompute:
133 return VK_SHADER_STAGE_COMPUTE_BIT;
134 case spv::ExecutionModelRayGenerationNV:
135 return VK_SHADER_STAGE_RAYGEN_BIT_NV;
136 case spv::ExecutionModelAnyHitNV:
137 return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
138 case spv::ExecutionModelClosestHitNV:
139 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
140 case spv::ExecutionModelMissNV:
141 return VK_SHADER_STAGE_MISS_BIT_NV;
142 case spv::ExecutionModelIntersectionNV:
143 return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
144 case spv::ExecutionModelCallableNV:
145 return VK_SHADER_STAGE_CALLABLE_BIT_NV;
146 case spv::ExecutionModelTaskNV:
147 return VK_SHADER_STAGE_TASK_BIT_NV;
148 case spv::ExecutionModelMeshNV:
149 return VK_SHADER_STAGE_MESH_BIT_NV;
150 default:
151 return 0;
152 }
153}
154
155// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
156// important for identifying the set of shader resources actually used by an entrypoint, for example.
157// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
158// - NOT the shader input/output interfaces.
159//
160// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
161// converting parts of this to be generated from the machine-readable spec instead.
162layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const {
163 layer_data::unordered_set<uint32_t> ids;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600164 if (entrypoint == end() || !has_valid_spirv) {
165 return ids;
166 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700167 layer_data::unordered_set<uint32_t> worklist;
168 worklist.insert(entrypoint.word(2));
169
170 while (!worklist.empty()) {
171 auto id_iter = worklist.begin();
172 auto id = *id_iter;
173 worklist.erase(id_iter);
174
175 auto insn = get_def(id);
176 if (insn == end()) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600177 // ID is something we didn't collect in SpirvStaticData. that's OK -- we'll stumble across all kinds of things here
sfricke-samsung962cad92021-04-13 00:46:29 -0700178 // that we may not care about.
179 continue;
180 }
181
182 // Try to add to the output set
183 if (!ids.insert(id).second) {
184 continue; // If we already saw this id, we don't want to walk it again.
185 }
186
187 switch (insn.opcode()) {
188 case spv::OpFunction:
189 // Scan whole body of the function, enlisting anything interesting
190 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
191 switch (insn.opcode()) {
192 case spv::OpLoad:
193 worklist.insert(insn.word(3)); // ptr
194 break;
195 case spv::OpStore:
196 worklist.insert(insn.word(1)); // ptr
197 break;
198 case spv::OpAccessChain:
199 case spv::OpInBoundsAccessChain:
200 worklist.insert(insn.word(3)); // base ptr
201 break;
202 case spv::OpSampledImage:
203 case spv::OpImageSampleImplicitLod:
204 case spv::OpImageSampleExplicitLod:
205 case spv::OpImageSampleDrefImplicitLod:
206 case spv::OpImageSampleDrefExplicitLod:
207 case spv::OpImageSampleProjImplicitLod:
208 case spv::OpImageSampleProjExplicitLod:
209 case spv::OpImageSampleProjDrefImplicitLod:
210 case spv::OpImageSampleProjDrefExplicitLod:
211 case spv::OpImageFetch:
212 case spv::OpImageGather:
213 case spv::OpImageDrefGather:
214 case spv::OpImageRead:
215 case spv::OpImage:
216 case spv::OpImageQueryFormat:
217 case spv::OpImageQueryOrder:
218 case spv::OpImageQuerySizeLod:
219 case spv::OpImageQuerySize:
220 case spv::OpImageQueryLod:
221 case spv::OpImageQueryLevels:
222 case spv::OpImageQuerySamples:
223 case spv::OpImageSparseSampleImplicitLod:
224 case spv::OpImageSparseSampleExplicitLod:
225 case spv::OpImageSparseSampleDrefImplicitLod:
226 case spv::OpImageSparseSampleDrefExplicitLod:
227 case spv::OpImageSparseSampleProjImplicitLod:
228 case spv::OpImageSparseSampleProjExplicitLod:
229 case spv::OpImageSparseSampleProjDrefImplicitLod:
230 case spv::OpImageSparseSampleProjDrefExplicitLod:
231 case spv::OpImageSparseFetch:
232 case spv::OpImageSparseGather:
233 case spv::OpImageSparseDrefGather:
234 case spv::OpImageTexelPointer:
235 worklist.insert(insn.word(3)); // Image or sampled image
236 break;
237 case spv::OpImageWrite:
238 worklist.insert(insn.word(1)); // Image -- different operand order to above
239 break;
240 case spv::OpFunctionCall:
241 for (uint32_t i = 3; i < insn.len(); i++) {
242 worklist.insert(insn.word(i)); // fn itself, and all args
243 }
244 break;
245
246 case spv::OpExtInst:
247 for (uint32_t i = 5; i < insn.len(); i++) {
248 worklist.insert(insn.word(i)); // Operands to ext inst
249 }
250 break;
251
252 default: {
253 if (AtomicOperation(insn.opcode())) {
254 if (insn.opcode() == spv::OpAtomicStore) {
255 worklist.insert(insn.word(1)); // ptr
256 } else {
257 worklist.insert(insn.word(3)); // ptr
258 }
259 }
260 break;
261 }
262 }
263 }
264 break;
265 }
266 }
267
268 return ids;
269}
270
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600271layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology(const spirv_inst_iter &entrypoint) const {
272 layer_data::optional<VkPrimitiveTopology> result;
273
sfricke-samsung962cad92021-04-13 00:46:29 -0700274 auto entrypoint_id = entrypoint.word(2);
275 bool is_point_mode = false;
276
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600277 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
278 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700279 for (auto insn : it->second) {
280 switch (insn.word(2)) {
281 case spv::ExecutionModePointMode:
282 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
283 is_point_mode = true;
284 break;
285
286 case spv::ExecutionModeOutputPoints:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600287 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
sfricke-samsung962cad92021-04-13 00:46:29 -0700288 break;
289
290 case spv::ExecutionModeIsolines:
291 case spv::ExecutionModeOutputLineStrip:
Ricardo Garcia122f8f02021-09-28 16:47:19 +0200292 case spv::ExecutionModeOutputLinesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600293 result.emplace(VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700294 break;
295
296 case spv::ExecutionModeTriangles:
297 case spv::ExecutionModeQuads:
298 case spv::ExecutionModeOutputTriangleStrip:
299 case spv::ExecutionModeOutputTrianglesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600300 result.emplace(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700301 break;
302 }
303 }
304 }
305
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600306 if (is_point_mode) {
307 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
308 }
309
310 return result;
sfricke-samsung962cad92021-04-13 00:46:29 -0700311}
312
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600313SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &mod) {
314 for (auto insn : mod) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700315 switch (insn.opcode()) {
316 // Types
317 case spv::OpTypeVoid:
318 case spv::OpTypeBool:
319 case spv::OpTypeInt:
320 case spv::OpTypeFloat:
321 case spv::OpTypeVector:
322 case spv::OpTypeMatrix:
323 case spv::OpTypeImage:
324 case spv::OpTypeSampler:
325 case spv::OpTypeSampledImage:
326 case spv::OpTypeArray:
327 case spv::OpTypeRuntimeArray:
328 case spv::OpTypeStruct:
329 case spv::OpTypeOpaque:
330 case spv::OpTypePointer:
331 case spv::OpTypeFunction:
332 case spv::OpTypeEvent:
333 case spv::OpTypeDeviceEvent:
334 case spv::OpTypeReserveId:
335 case spv::OpTypeQueue:
336 case spv::OpTypePipe:
337 case spv::OpTypeAccelerationStructureNV:
338 case spv::OpTypeCooperativeMatrixNV:
339 def_index[insn.word(1)] = insn.offset();
340 break;
341
342 // Fixed constants
343 case spv::OpConstantTrue:
344 case spv::OpConstantFalse:
345 case spv::OpConstant:
346 case spv::OpConstantComposite:
347 case spv::OpConstantSampler:
348 case spv::OpConstantNull:
349 def_index[insn.word(2)] = insn.offset();
350 break;
351
352 // Specialization constants
353 case spv::OpSpecConstantTrue:
354 case spv::OpSpecConstantFalse:
355 case spv::OpSpecConstant:
356 case spv::OpSpecConstantComposite:
357 case spv::OpSpecConstantOp:
358 def_index[insn.word(2)] = insn.offset();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600359 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700360 break;
361
sfricke-samsung58b84352021-07-31 21:41:04 -0700362 // Have a result that can be a pointer
sfricke-samsung962cad92021-04-13 00:46:29 -0700363 case spv::OpVariable:
sfricke-samsung58b84352021-07-31 21:41:04 -0700364 case spv::OpAccessChain:
365 case spv::OpInBoundsAccessChain:
366 case spv::OpFunctionParameter:
367 case spv::OpImageTexelPointer:
sfricke-samsung962cad92021-04-13 00:46:29 -0700368 def_index[insn.word(2)] = insn.offset();
369 break;
370
371 // Functions
372 case spv::OpFunction:
373 def_index[insn.word(2)] = insn.offset();
sfricke-samsung962cad92021-04-13 00:46:29 -0700374 break;
375
376 // Decorations
377 case spv::OpDecorate: {
378 auto target_id = insn.word(1);
379 decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
380 decoration_inst.push_back(insn);
381 if (insn.word(2) == spv::DecorationBuiltIn) {
382 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3)));
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600383 } else if (insn.word(2) == spv::DecorationSpecId) {
384 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700385 }
386
387 } break;
388 case spv::OpGroupDecorate: {
389 auto const &src = decorations[insn.word(1)];
390 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600391 has_group_decoration = true;
392 } break;
393 case spv::OpDecorationGroup:
394 case spv::OpGroupMemberDecorate: {
395 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700396 } break;
397 case spv::OpMemberDecorate: {
398 member_decoration_inst.push_back(insn);
399 if (insn.word(3) == spv::DecorationBuiltIn) {
400 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4)));
401 }
402 } break;
403
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600404 // Copy operations
405 case spv::OpCopyLogical:
406 case spv::OpCopyObject: {
407 def_index[insn.word(2)] = insn.offset();
408 break;
409 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700410
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600411 // Execution Mode
412 case spv::OpExecutionMode: {
413 execution_mode_inst[insn.word(1)].push_back(insn);
414 } break;
415
416 case spv::OpLoad: {
417 def_index[insn.word(2)] = insn.offset();
418 } break;
419
420 default:
421 if (AtomicOperation(insn.opcode()) == true) {
422 // All atomics have a pointer referenced
423 spirv_inst_iter access;
424 if (insn.opcode() == spv::OpAtomicStore) {
425 access = mod.get_def(insn.word(1));
426 } else {
427 access = mod.get_def(insn.word(3));
428 def_index[insn.word(2)] = insn.offset();
429 }
430
431 atomic_instruction atomic;
432
433 auto pointer = mod.get_def(access.word(1));
434 // spirv-val should catch if not pointer
435 assert(pointer.opcode() == spv::OpTypePointer);
436 atomic.storage_class = pointer.word(2);
437
438 auto data_type = mod.get_def(pointer.word(3));
439 atomic.type = data_type.opcode();
440
441 // TODO - Should have a proper GetBitWidth like spirv-val does
442 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
443 atomic.bit_width = data_type.word(2);
444
445 atomic_inst[insn.offset()] = atomic;
446 }
447 // We don't care about any other defs for now.
448 break;
449 }
450 }
451
452 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(mod);
453 multiple_entry_points = entry_points.size() > 1;
454}
455
456// static
457std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
458 const SHADER_MODULE_STATE &mod) {
459 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
460 function_set func_set = {};
461 EntryPoint *entry_point = nullptr;
462
463 for (auto insn : mod) {
464 // offset is not 0, it means it's updated and the offset is in a Function.
465 if (func_set.offset) {
466 func_set.op_lists.emplace(insn.opcode(), insn.offset());
467 } else if (entry_point) {
468 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
469 }
470
471 switch (insn.opcode()) {
472 // Functions
473 case spv::OpFunction:
474 func_set.id = insn.word(2);
475 func_set.offset = insn.offset();
476 func_set.op_lists.clear();
477 break;
478
479 // Entry points ... add to the entrypoint table
480 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700481 // Entry points do not have an id (the id is the function id) and thus need their own table
482 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
483 auto execution_model = insn.word(1);
484 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
485 entry_points.emplace(entrypoint_name,
486 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
487
488 auto range = entry_points.equal_range(entrypoint_name);
489 for (auto it = range.first; it != range.second; ++it) {
490 if (it->second.offset == insn.offset()) {
491 entry_point = &(it->second);
492 break;
493 }
494 }
495 assert(entry_point != nullptr);
496 break;
497 }
498 case spv::OpFunctionEnd: {
499 assert(entry_point != nullptr);
500 func_set.length = insn.offset() - func_set.offset;
501 entry_point->function_set_list.emplace_back(func_set);
502 break;
503 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700504 }
505 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600506
507 SHADER_MODULE_STATE::SetPushConstantUsedInShader(mod, entry_points);
508 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700509}
510
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600511void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
512 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700513 spvtools::Optimizer optimizer(env);
514 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
515 std::vector<uint32_t> optimized_binary;
516 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600517 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
518
sfricke-samsung962cad92021-04-13 00:46:29 -0700519 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600520 // NOTE: We need to update words with the result from the spirv-tools optimizer.
521 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
522 // of the SHADER_MODULE_STATE instance.
523 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700524 }
525 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700526}
527
sfricke-samsung58b84352021-07-31 21:41:04 -0700528char const *StorageClassName(unsigned sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700529 switch (sc) {
530 case spv::StorageClassInput:
531 return "input";
532 case spv::StorageClassOutput:
533 return "output";
534 case spv::StorageClassUniformConstant:
535 return "const uniform";
536 case spv::StorageClassUniform:
537 return "uniform";
538 case spv::StorageClassWorkgroup:
539 return "workgroup local";
540 case spv::StorageClassCrossWorkgroup:
541 return "workgroup global";
542 case spv::StorageClassPrivate:
543 return "private global";
544 case spv::StorageClassFunction:
545 return "function";
546 case spv::StorageClassGeneric:
547 return "generic";
548 case spv::StorageClassAtomicCounter:
549 return "atomic counter";
550 case spv::StorageClassImage:
551 return "image";
552 case spv::StorageClassPushConstant:
553 return "push constant";
554 case spv::StorageClassStorageBuffer:
555 return "storage buffer";
556 default:
557 return "unknown";
558 }
559}
560
561void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, unsigned type) const {
562 auto insn = get_def(type);
563 assert(insn != end());
564
565 switch (insn.opcode()) {
566 case spv::OpTypeBool:
567 ss << "bool";
568 break;
569 case spv::OpTypeInt:
570 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
571 break;
572 case spv::OpTypeFloat:
573 ss << "float" << insn.word(2);
574 break;
575 case spv::OpTypeVector:
576 ss << "vec" << insn.word(3) << " of ";
577 DescribeTypeInner(ss, insn.word(2));
578 break;
579 case spv::OpTypeMatrix:
580 ss << "mat" << insn.word(3) << " of ";
581 DescribeTypeInner(ss, insn.word(2));
582 break;
583 case spv::OpTypeArray:
584 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
585 DescribeTypeInner(ss, insn.word(2));
586 break;
587 case spv::OpTypeRuntimeArray:
588 ss << "runtime arr[] of ";
589 DescribeTypeInner(ss, insn.word(2));
590 break;
591 case spv::OpTypePointer:
592 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
593 DescribeTypeInner(ss, insn.word(3));
594 break;
595 case spv::OpTypeStruct: {
596 ss << "struct of (";
597 for (unsigned i = 2; i < insn.len(); i++) {
598 DescribeTypeInner(ss, insn.word(i));
599 if (i == insn.len() - 1) {
600 ss << ")";
601 } else {
602 ss << ", ";
603 }
604 }
605 break;
606 }
607 case spv::OpTypeSampler:
608 ss << "sampler";
609 break;
610 case spv::OpTypeSampledImage:
611 ss << "sampler+";
612 DescribeTypeInner(ss, insn.word(2));
613 break;
614 case spv::OpTypeImage:
615 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
616 break;
617 case spv::OpTypeAccelerationStructureNV:
618 ss << "accelerationStruture";
619 break;
620 default:
621 ss << "oddtype";
622 break;
623 }
624}
625
626std::string SHADER_MODULE_STATE::DescribeType(unsigned type) const {
627 std::ostringstream ss;
628 DescribeTypeInner(ss, type);
629 return ss.str();
630}
631
632const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
633 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600634 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700635 for (auto it = range.first; it != range.second; ++it) {
636 if (it->second.stage == stageBits) {
637 return &(it->second);
638 }
639 }
640 return nullptr;
641}
642
643spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600644 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700645 for (auto it = range.first; it != range.second; ++it) {
646 if (it->second.stage == stageBits) {
647 return at(it->second.offset);
648 }
649 }
650 return end();
651}
652
653// Because the following is legal, need the entry point
654// OpEntryPoint GLCompute %main "name_a"
655// OpEntryPoint GLCompute %main "name_b"
656bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
657 uint32_t &local_size_z) const {
658 auto entrypoint_id = entrypoint.word(2);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600659 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
660 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700661 for (auto insn : it->second) {
662 // Future Note: For now, Vulkan doesn't have a valid mode that can makes use of OpExecutionModeId
663 // In the future if something like LocalSizeId is supported, the <id> will need to be checked also
664 assert(insn.opcode() == spv::OpExecutionMode);
665 if (insn.word(2) == spv::ExecutionModeLocalSize) {
666 local_size_x = insn.word(3);
667 local_size_y = insn.word(4);
668 local_size_z = insn.word(5);
669 return true;
670 }
671 }
672 }
673 return false;
674}
675
676// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
677// Otherwise, returns src->end().
678spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(unsigned id) const {
679 auto value = get_def(id);
680
681 // If id is a copy, see where it was copied from
682 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
683 id = value.word(3);
684 value = get_def(id);
685 }
686
687 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
688 return value;
689 }
690 return end();
691}
692
693// Either returns the constant value described by the instruction at id, or 1
694uint32_t SHADER_MODULE_STATE::GetConstantValueById(unsigned id) const {
695 auto value = GetConstantDef(id);
696
697 if (end() == value) {
698 // TODO: Either ensure that the specialization transform is already performed on a module we're
699 // considering here, OR -- specialize on the fly now.
700 return 1;
701 }
702 return GetConstantValue(value);
703}
704
705// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
706// negative.
707int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
708 auto type = get_def(resource.type_id);
709 while (true) {
710 switch (type.opcode()) {
711 case spv::OpTypeSampledImage:
712 type = get_def(type.word(2));
713 break;
714 case spv::OpTypePointer:
715 type = get_def(type.word(3));
716 break;
717 case spv::OpTypeImage:
718 return type.word(3);
719 default:
720 return -1;
721 }
722 }
723}
724
725unsigned SHADER_MODULE_STATE::GetLocationsConsumedByType(unsigned type, bool strip_array_level) const {
726 auto insn = get_def(type);
727 assert(insn != end());
728
729 switch (insn.opcode()) {
730 case spv::OpTypePointer:
731 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
732 // pointers around.
733 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
734 case spv::OpTypeArray:
735 if (strip_array_level) {
736 return GetLocationsConsumedByType(insn.word(2), false);
737 } else {
738 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
739 }
740 case spv::OpTypeMatrix:
741 // Num locations is the dimension * element size
742 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
743 case spv::OpTypeVector: {
744 auto scalar_type = get_def(insn.word(2));
745 auto bit_width =
746 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
747
748 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
749 return (bit_width * insn.word(3) + 127) / 128;
750 }
751 default:
752 // Everything else is just 1.
753 return 1;
754
755 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
756 }
757}
758
759unsigned SHADER_MODULE_STATE::GetComponentsConsumedByType(unsigned type, bool strip_array_level) const {
760 auto insn = get_def(type);
761 assert(insn != end());
762
763 switch (insn.opcode()) {
764 case spv::OpTypePointer:
765 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
766 // pointers around.
767 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
768 case spv::OpTypeStruct: {
769 uint32_t sum = 0;
770 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
771 sum += GetComponentsConsumedByType(insn.word(i), false);
772 }
773 return sum;
774 }
775 case spv::OpTypeArray:
776 if (strip_array_level) {
777 return GetComponentsConsumedByType(insn.word(2), false);
778 } else {
779 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
780 }
781 case spv::OpTypeMatrix:
782 // Num locations is the dimension * element size
783 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
784 case spv::OpTypeVector: {
785 auto scalar_type = get_def(insn.word(2));
786 auto bit_width =
787 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
788 // One component is 32-bit
789 return (bit_width * insn.word(3) + 31) / 32;
790 }
791 case spv::OpTypeFloat: {
792 auto bit_width = insn.word(2);
793 return (bit_width + 31) / 32;
794 }
795 case spv::OpTypeInt: {
796 auto bit_width = insn.word(2);
797 return (bit_width + 31) / 32;
798 }
799 case spv::OpConstant:
800 return GetComponentsConsumedByType(insn.word(1), false);
801 default:
802 return 0;
803 }
804}
805
806// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
807// also used for input attachments, as we statically know their format.
808unsigned SHADER_MODULE_STATE::GetFundamentalType(unsigned type) const {
809 auto insn = get_def(type);
810 assert(insn != end());
811
812 switch (insn.opcode()) {
813 case spv::OpTypeInt:
814 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
815 case spv::OpTypeFloat:
816 return FORMAT_TYPE_FLOAT;
817 case spv::OpTypeVector:
818 case spv::OpTypeMatrix:
819 case spv::OpTypeArray:
820 case spv::OpTypeRuntimeArray:
821 case spv::OpTypeImage:
822 return GetFundamentalType(insn.word(2));
823 case spv::OpTypePointer:
824 return GetFundamentalType(insn.word(3));
825
826 default:
827 return 0;
828 }
829}
830
831spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
832 while (true) {
833 if (def.opcode() == spv::OpTypePointer) {
834 def = get_def(def.word(3));
835 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
836 def = get_def(def.word(2));
837 is_array_of_verts = false;
838 } else if (def.opcode() == spv::OpTypeStruct) {
839 return def;
840 } else {
841 return end();
842 }
843 }
844}
845
846void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &memberDecorate_offsets,
847 shader_struct_member &data) const {
848 const auto struct_it = GetStructType(it, false);
849 assert(struct_it != end());
850 data.size = 0;
851
852 shader_struct_member data1;
853 uint32_t i = 2;
854 uint32_t local_offset = 0;
855 std::vector<uint32_t> offsets;
856 offsets.resize(struct_it.len() - i);
857
858 // The members of struct in SPRIV_R aren't always sort, so we need to know their order.
859 for (const auto offset : memberDecorate_offsets) {
860 const auto member_decorate = at(offset);
861 if (member_decorate.word(1) != struct_it.word(1)) {
862 continue;
863 }
864
865 offsets[member_decorate.word(2)] = member_decorate.word(4);
866 }
867
868 for (const auto offset : offsets) {
869 local_offset = offset;
870 data1 = {};
871 data1.root = data.root;
872 data1.offset = local_offset;
873 auto def_member = get_def(struct_it.word(i));
874
875 // Array could be multi-dimensional
876 while (def_member.opcode() == spv::OpTypeArray) {
877 const auto len_id = def_member.word(3);
878 const auto def_len = get_def(len_id);
879 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
880 def_member = get_def(def_member.word(2));
881 }
882
883 if (def_member.opcode() == spv::OpTypeStruct) {
884 DefineStructMember(def_member, memberDecorate_offsets, data1);
885 } else if (def_member.opcode() == spv::OpTypePointer) {
886 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
887 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
888 // that "points to something."
889 data1.size = 8;
890 } else {
891 // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct
892 DefineStructMember(def_member, memberDecorate_offsets, data1);
893 }
894 } else {
895 if (def_member.opcode() == spv::OpTypeMatrix) {
896 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
897 def_member = get_def(def_member.word(2));
898 }
899
900 if (def_member.opcode() == spv::OpTypeVector) {
901 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
902 def_member = get_def(def_member.word(2));
903 }
904
905 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
906 data1.size = (def_member.word(2) / 8);
907 }
908 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
909 if (array_length_hierarchy_szie > 0) {
910 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
911
912 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
913 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
914 }
915 }
916 data.struct_members.emplace_back(data1);
917 ++i;
918 }
919 uint32_t total_array_length = 1;
920 for (const auto length : data1.array_length_hierarchy) {
921 total_array_length *= length;
922 }
923 data.size = local_offset + data1.size * total_array_length;
924}
925
926static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
927 int array_indices_size = static_cast<int>(array_indices.size());
928 if (array_indices_size) {
929 uint32_t array_index = 0;
930 uint32_t i = 0;
931 for (const auto index : array_indices) {
932 array_index += (data.array_block_size[i] * index);
933 ++i;
934 }
935 offset += (array_index * data.size);
936 }
937 return offset;
938}
939
940static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
941 int array_indices_size = static_cast<int>(array_indices.size());
942 uint32_t block_memory_size = data.size;
943 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
944 block_memory_size *= data.array_length_hierarchy[i];
945 }
946
947 offset = UpdateOffset(offset, array_indices, data);
948
949 uint32_t end = offset + block_memory_size;
950 auto used_bytes = data.GetUsedbytes();
951 if (used_bytes->size() < end) {
952 used_bytes->resize(end, 0);
953 }
954 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
955}
956
957void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
958 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
959 if (access_chain_word_index < access_chain_it.len()) {
960 if (data.array_length_hierarchy.size() > array_indices.size()) {
961 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
962 ++access_chain_word_index;
963
964 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
965 array_indices.emplace_back(def_it.word(3));
966 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
967 } else {
968 // If it is a variable, set the all array is used.
969 if (access_chain_word_index < access_chain_it.len()) {
970 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
971 for (uint32_t i = 0; i < array_length; ++i) {
972 auto array_indices2 = array_indices;
973 array_indices2.emplace_back(i);
974 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
975 }
976 } else {
977 SetUsedBytes(offset, array_indices, data);
978 }
979 }
980 } else {
981 offset = UpdateOffset(offset, array_indices, data);
982 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
983 }
984 } else {
985 SetUsedBytes(offset, array_indices, data);
986 }
987}
988
989void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
990 const shader_struct_member &data) const {
991 std::vector<uint32_t> array_indices_emptry;
992
993 if (access_chain_word_index < access_chain_it.len()) {
994 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
995 ++access_chain_word_index;
996
997 auto data1 = data.struct_members[strcut_member_index];
998 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
999 }
1000}
1001
1002void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
1003 const shader_struct_member &data) const {
1004 for (const auto &func_set : function_set_list) {
1005 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
1006 for (auto it = range.first; it != range.second; ++it) {
1007 auto access_chain = at(it->second);
1008 if (access_chain.word(3) == variable_id) {
1009 RunUsedStruct(0, 4, access_chain, data);
1010 }
1011 }
1012 }
1013}
1014
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001015// static
1016void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
1017 const SHADER_MODULE_STATE &mod, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001018 for (auto &entrypoint : entry_points) {
1019 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1020 for (auto it = range.first; it != range.second; ++it) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001021 const auto def_insn = mod.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001022
1023 if (def_insn.word(3) == spv::StorageClassPushConstant) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001024 spirv_inst_iter type = mod.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001025 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1026 std::vector<uint32_t> offsets;
1027
1028 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001029 auto member_decorate = mod.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001030 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1031 offsets.emplace_back(member_decorate.offset());
1032 }
1033 }
1034 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001035 mod.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1036 mod.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1037 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001038 }
1039 }
1040 }
1041}
1042
1043uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1044 auto type = get_def(type_id);
1045
1046 while (true) {
1047 switch (type.opcode()) {
1048 case spv::OpTypeArray:
1049 case spv::OpTypeRuntimeArray:
1050 case spv::OpTypeSampledImage:
1051 type = get_def(type.word(2));
1052 break;
1053 case spv::OpTypePointer:
1054 type = get_def(type.word(3));
1055 break;
1056 case spv::OpTypeImage: {
1057 auto dim = type.word(3);
1058 auto arrayed = type.word(5);
1059 auto msaa = type.word(6);
1060
1061 uint32_t bits = 0;
1062 switch (GetFundamentalType(type.word(2))) {
1063 case FORMAT_TYPE_FLOAT:
1064 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1065 break;
1066 case FORMAT_TYPE_UINT:
1067 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1068 break;
1069 case FORMAT_TYPE_SINT:
1070 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1071 break;
1072 default:
1073 break;
1074 }
1075
1076 switch (dim) {
1077 case spv::Dim1D:
1078 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1079 return bits;
1080 case spv::Dim2D:
1081 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1082 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1083 return bits;
1084 case spv::Dim3D:
1085 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1086 return bits;
1087 case spv::DimCube:
1088 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1089 return bits;
1090 case spv::DimSubpassData:
1091 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1092 return bits;
1093 default: // buffer, etc.
1094 return bits;
1095 }
1096 }
1097 default:
1098 return 0;
1099 }
1100 }
1101}
1102
1103// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1104// This function examines instructions in the static call tree for a write to this variable.
1105bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1106 auto type = builtin_instr.opcode();
1107 uint32_t target_id = builtin_instr.word(1);
1108 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001109 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001110
1111 if (type == spv::OpMemberDecorate) {
1112 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1113 auto insn = entrypoint;
1114 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1115 switch (insn.opcode()) {
1116 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001117 if (insn.word(2) == spv::StorageClassOutput) {
1118 const auto type_id = insn.word(3);
1119 if (type_id == target_id) {
1120 target_id = insn.word(1);
1121 } else {
1122 // If the output is an array, check if the element type is what we're looking for
1123 const auto type_insn = get_def(type_id);
1124 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1125 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001126 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001127 }
1128 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001129 }
1130 break;
1131 case spv::OpVariable:
1132 if (insn.word(1) == target_id) {
1133 target_id = insn.word(2);
1134 init_complete = true;
1135 }
1136 break;
1137 }
1138 insn++;
1139 }
1140 }
1141
1142 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1143
1144 bool found_write = false;
1145 layer_data::unordered_set<uint32_t> worklist;
1146 worklist.insert(entrypoint.word(2));
1147
1148 // Follow instructions in call graph looking for writes to target
1149 while (!worklist.empty() && !found_write) {
1150 auto id_iter = worklist.begin();
1151 auto id = *id_iter;
1152 worklist.erase(id_iter);
1153
1154 auto insn = get_def(id);
1155 if (insn == end()) {
1156 continue;
1157 }
1158
1159 if (insn.opcode() == spv::OpFunction) {
1160 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001161 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001162 switch (insn.opcode()) {
1163 case spv::OpAccessChain:
1164 if (insn.word(3) == target_id) {
1165 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001166 // Get the target member of the struct
1167 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1168 // arrays of structs of structs) is not currently supported.
1169 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1170 if (value_itr != end()) {
1171 auto value = GetConstantValue(value_itr);
1172 if (value == builtin_instr.word(2)) {
1173 target_id = insn.word(2);
1174 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001175 }
1176 } else {
1177 target_id = insn.word(2);
1178 }
1179 }
1180 break;
1181 case spv::OpStore:
1182 if (insn.word(1) == target_id) {
1183 found_write = true;
1184 }
1185 break;
1186 case spv::OpFunctionCall:
1187 worklist.insert(insn.word(3));
1188 break;
1189 }
1190 }
1191 }
1192 }
1193 return found_write;
1194}
1195
1196// Used by the collection functions to help aid in state tracking
1197struct shader_module_used_operators {
1198 bool updated;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001199 std::vector<unsigned> imagread_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001200 std::vector<unsigned> imagwrite_members;
1201 std::vector<unsigned> atomic_members;
1202 std::vector<unsigned> store_members;
1203 std::vector<unsigned> atomic_store_members;
1204 std::vector<unsigned> sampler_implicitLod_dref_proj_members; // sampler Load id
1205 std::vector<unsigned> sampler_bias_offset_members; // sampler Load id
1206 std::vector<std::pair<unsigned, unsigned>> sampledImage_members; // <image,sampler> Load id
1207 layer_data::unordered_map<unsigned, unsigned> load_members;
1208 layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> accesschain_members;
1209 layer_data::unordered_map<unsigned, unsigned> image_texel_pointer_members;
1210
1211 shader_module_used_operators() : updated(false) {}
1212
1213 bool CheckImageOperandsBiasOffset(uint32_t type) {
1214 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1215 spv::ImageOperandsConstOffsetsMask)
1216 ? true
1217 : false;
1218 }
1219
1220 void update(SHADER_MODULE_STATE const *module) {
1221 if (updated) return;
1222 updated = true;
1223
1224 for (auto insn : *module) {
1225 switch (insn.opcode()) {
1226 case spv::OpImageSampleImplicitLod:
1227 case spv::OpImageSampleProjImplicitLod:
1228 case spv::OpImageSampleProjExplicitLod:
1229 case spv::OpImageSparseSampleImplicitLod:
1230 case spv::OpImageSparseSampleProjImplicitLod:
1231 case spv::OpImageSparseSampleProjExplicitLod: {
1232 sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id
1233 // ImageOperands in index: 5
1234 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
1235 sampler_bias_offset_members.emplace_back(insn.word(3));
1236 }
1237 break;
1238 }
1239 case spv::OpImageSampleDrefImplicitLod:
1240 case spv::OpImageSampleDrefExplicitLod:
1241 case spv::OpImageSampleProjDrefImplicitLod:
1242 case spv::OpImageSampleProjDrefExplicitLod:
1243 case spv::OpImageSparseSampleDrefImplicitLod:
1244 case spv::OpImageSparseSampleDrefExplicitLod:
1245 case spv::OpImageSparseSampleProjDrefImplicitLod:
1246 case spv::OpImageSparseSampleProjDrefExplicitLod: {
1247 sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id
1248 // ImageOperands in index: 6
1249 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
1250 sampler_bias_offset_members.emplace_back(insn.word(3));
1251 }
1252 break;
1253 }
1254 case spv::OpImageSampleExplicitLod:
1255 case spv::OpImageSparseSampleExplicitLod: {
1256 // ImageOperands in index: 5
1257 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
1258 sampler_bias_offset_members.emplace_back(insn.word(3));
1259 }
1260 break;
1261 }
1262 case spv::OpStore: {
1263 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1264 break;
1265 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001266 case spv::OpImageRead:
1267 case spv::OpImageSparseRead: {
1268 imagread_members.emplace_back(insn.word(3)); // Load id
1269 break;
1270 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001271 case spv::OpImageWrite: {
1272 imagwrite_members.emplace_back(insn.word(1)); // Load id
1273 break;
1274 }
1275 case spv::OpSampledImage: {
1276 // 3: image load id, 4: sampler load id
1277 sampledImage_members.emplace_back(std::pair<unsigned, unsigned>(insn.word(3), insn.word(4)));
1278 break;
1279 }
1280 case spv::OpLoad: {
1281 // 2: Load id, 3: object id or AccessChain id
1282 load_members.emplace(insn.word(2), insn.word(3));
1283 break;
1284 }
1285 case spv::OpAccessChain: {
1286 if (insn.len() == 4) {
1287 // If it is for struct, the length is only 4.
1288 // 2: AccessChain id, 3: object id
1289 accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), 0));
1290 } else {
1291 // 2: AccessChain id, 3: object id, 4: object id of array index
1292 accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), insn.word(4)));
1293 }
1294 break;
1295 }
1296 case spv::OpImageTexelPointer: {
1297 // 2: ImageTexelPointer id, 3: object id
1298 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1299 break;
1300 }
1301 default: {
1302 if (AtomicOperation(insn.opcode())) {
1303 if (insn.opcode() == spv::OpAtomicStore) {
1304 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1305 } else {
1306 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1307 }
1308 }
1309 break;
1310 }
1311 }
1312 }
1313 }
1314};
1315
1316static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<unsigned> &operator_members,
1317 const layer_data::unordered_map<unsigned, unsigned> &load_members,
1318 const layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> &accesschain_members) {
1319 for (auto load_id : operator_members) {
1320 if (object_id == load_id) return true;
1321 auto load_it = load_members.find(load_id);
1322 if (load_it == load_members.end()) {
1323 continue;
1324 }
1325 if (load_it->second == object_id) {
1326 return true;
1327 }
1328
1329 auto accesschain_it = accesschain_members.find(load_it->second);
1330 if (accesschain_it == accesschain_members.end()) {
1331 continue;
1332 }
1333 if (accesschain_it->second.first == object_id) {
1334 return true;
1335 }
1336 }
1337 return false;
1338}
1339
1340// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1341// atomic operation, matching images to samplers, etc
1342void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1343 interface_var &out_interface_var,
1344 shader_module_used_operators &used_operators) const {
1345 uint32_t type_id = id_it.word(1);
1346 unsigned int id = id_it.word(2);
1347
1348 auto type = get_def(type_id);
1349
1350 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1351 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1352 type.opcode() == spv::OpTypeSampledImage) {
1353 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1354 type.opcode() == spv::OpTypeSampledImage) {
1355 type = get_def(type.word(2)); // Element type
1356 } else {
1357 type = get_def(type.word(3)); // Pointer type
1358 }
1359 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001360
sfricke-samsung962cad92021-04-13 00:46:29 -07001361 switch (type.opcode()) {
1362 case spv::OpTypeImage: {
1363 auto dim = type.word(3);
1364 if (dim != spv::DimSubpassData) {
1365 used_operators.update(this);
1366
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001367 // Sampled == 2 indicates used without a sampler (a storage image)
1368 bool is_image_without_format = false;
1369 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1370
sfricke-samsung962cad92021-04-13 00:46:29 -07001371 if (CheckObjectIDFromOpLoad(id, used_operators.imagwrite_members, used_operators.load_members,
1372 used_operators.accesschain_members)) {
1373 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001374 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1375 }
1376 if (CheckObjectIDFromOpLoad(id, used_operators.imagread_members, used_operators.load_members,
1377 used_operators.accesschain_members)) {
1378 out_interface_var.is_readable = true;
1379 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001380 }
1381 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1382 used_operators.accesschain_members)) {
1383 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1384 }
1385 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1386 used_operators.accesschain_members)) {
1387 out_interface_var.is_sampler_bias_offset = true;
1388 }
1389 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1390 used_operators.accesschain_members) ||
1391 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1392 used_operators.accesschain_members)) {
1393 out_interface_var.is_atomic_operation = true;
1394 }
1395
1396 for (auto &itp_id : used_operators.sampledImage_members) {
1397 // Find if image id match.
1398 uint32_t image_index = 0;
1399 auto load_it = used_operators.load_members.find(itp_id.first);
1400 if (load_it == used_operators.load_members.end()) {
1401 continue;
1402 } else {
1403 if (load_it->second != id) {
1404 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1405 if (accesschain_it == used_operators.accesschain_members.end()) {
1406 continue;
1407 } else {
1408 if (accesschain_it->second.first != id) {
1409 continue;
1410 }
1411
1412 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1413 if (const_itr == end()) {
1414 // access chain index not a constant, skip.
1415 break;
1416 }
1417 image_index = GetConstantValue(const_itr);
1418 }
1419 }
1420 }
1421 // Find sampler's set binding.
1422 load_it = used_operators.load_members.find(itp_id.second);
1423 if (load_it == used_operators.load_members.end()) {
1424 continue;
1425 } else {
1426 uint32_t sampler_id = load_it->second;
1427 uint32_t sampler_index = 0;
1428 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1429
1430 if (accesschain_it != used_operators.accesschain_members.end()) {
1431 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1432 if (const_itr == end()) {
1433 // access chain index representing sampler index is not a constant, skip.
1434 break;
1435 }
1436 sampler_id = const_itr.offset();
1437 sampler_index = GetConstantValue(const_itr);
1438 }
1439 auto sampler_dec = get_decorations(sampler_id);
1440 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1441 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1442 }
1443 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001444 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001445 }
1446 }
1447 }
1448 return;
1449 }
1450
1451 case spv::OpTypeStruct: {
1452 layer_data::unordered_set<unsigned> nonwritable_members;
1453 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001454 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001455 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1456 nonwritable_members.insert(insn.word(2));
1457 }
1458 }
1459
1460 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1461 // as nonwritable.
1462 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1463 used_operators.update(this);
1464
1465 for (auto oid : used_operators.store_members) {
1466 if (id == oid) {
1467 out_interface_var.is_writable = true;
1468 return;
1469 }
1470 auto accesschain_it = used_operators.accesschain_members.find(oid);
1471 if (accesschain_it == used_operators.accesschain_members.end()) {
1472 continue;
1473 }
1474 if (accesschain_it->second.first == id) {
1475 out_interface_var.is_writable = true;
1476 return;
1477 }
1478 }
1479 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1480 used_operators.accesschain_members)) {
1481 out_interface_var.is_writable = true;
1482 return;
1483 }
1484 }
1485 }
1486 }
1487}
1488
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001489std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001490 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001491 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001492 shader_module_used_operators operators;
1493
1494 for (auto id : accessible_ids) {
1495 auto insn = get_def(id);
1496 assert(insn != end());
1497
1498 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001499 (insn.word(3) == spv::StorageClassUniform ||
1500 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001501 insn.word(3) == spv::StorageClassStorageBuffer)) {
1502 auto d = get_decorations(insn.word(2));
1503 unsigned set = d.descriptor_set;
1504 unsigned binding = d.binding;
1505
1506 interface_var v = {};
1507 v.id = insn.word(2);
1508 v.type_id = insn.word(1);
1509
1510 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1511 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001512 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001513 }
1514 }
1515
1516 return out;
1517}
1518
1519layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001520 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001521 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001522 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
1523 layer_data::unordered_set<unsigned> store_members;
1524 layer_data::unordered_map<unsigned, unsigned> accesschain_members;
1525
1526 for (auto insn : *this) {
1527 switch (insn.opcode()) {
1528 case spv::OpStore:
1529 case spv::OpAtomicStore: {
1530 store_members.insert(insn.word(1)); // object id or AccessChain id
1531 break;
1532 }
1533 case spv::OpAccessChain: {
1534 // 2: AccessChain id, 3: object id
1535 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1536 break;
1537 }
1538 default:
1539 break;
1540 }
1541 }
1542 if (store_members.empty()) {
1543 return location_list;
1544 }
1545 for (auto output : outputs) {
1546 auto store_it = store_members.find(output.second.id);
1547 if (store_it != store_members.end()) {
1548 location_list.insert(output.first.first);
1549 store_members.erase(store_it);
1550 continue;
1551 }
1552 store_it = store_members.begin();
1553 while (store_it != store_members.end()) {
1554 auto accesschain_it = accesschain_members.find(*store_it);
1555 if (accesschain_it == accesschain_members.end()) {
1556 ++store_it;
1557 continue;
1558 }
1559 if (accesschain_it->second == output.second.id) {
1560 location_list.insert(output.first.first);
1561 store_members.erase(store_it);
1562 accesschain_members.erase(accesschain_it);
1563 break;
1564 }
1565 ++store_it;
1566 }
1567 }
1568 return location_list;
1569}
1570
1571bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001572 uint32_t id, uint32_t type_id, bool is_patch,
1573 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001574 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1575 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1576 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1577 // This isn't an interface block.
1578 return false;
1579 }
1580
1581 layer_data::unordered_map<unsigned, unsigned> member_components;
1582 layer_data::unordered_map<unsigned, unsigned> member_relaxed_precision;
1583 layer_data::unordered_map<unsigned, unsigned> member_patch;
1584
1585 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001586 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001587 if (insn.word(1) == type.word(1)) {
1588 unsigned member_index = insn.word(2);
1589
1590 if (insn.word(3) == spv::DecorationComponent) {
1591 unsigned component = insn.word(4);
1592 member_components[member_index] = component;
1593 }
1594
1595 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1596 member_relaxed_precision[member_index] = 1;
1597 }
1598
1599 if (insn.word(3) == spv::DecorationPatch) {
1600 member_patch[member_index] = 1;
1601 }
1602 }
1603 }
1604
1605 // TODO: correctly handle location assignment from outside
1606
1607 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001608 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001609 if (insn.word(1) == type.word(1)) {
1610 unsigned member_index = insn.word(2);
1611 unsigned member_type_id = type.word(2 + member_index);
1612
1613 if (insn.word(3) == spv::DecorationLocation) {
1614 unsigned location = insn.word(4);
1615 unsigned num_locations = GetLocationsConsumedByType(member_type_id, false);
1616 auto component_it = member_components.find(member_index);
1617 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
1618 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1619 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1620
1621 for (unsigned int offset = 0; offset < num_locations; offset++) {
1622 interface_var v = {};
1623 v.id = id;
1624 // TODO: member index in interface_var too?
1625 v.type_id = member_type_id;
1626 v.offset = offset;
1627 v.is_patch = member_is_patch;
1628 v.is_block_member = true;
1629 v.is_relaxed_precision = is_relaxed_precision;
1630 (*out)[std::make_pair(location + offset, component)] = v;
1631 }
1632 }
1633 }
1634 }
1635
1636 return true;
1637}
1638
1639std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1640 spv::StorageClass sinterface,
1641 bool is_array_of_verts) const {
1642 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1643
1644 std::map<location_t, interface_var> out;
1645
1646 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1647 auto insn = get_def(iid);
1648 assert(insn != end());
1649 assert(insn.opcode() == spv::OpVariable);
1650
ziga-lunarg9e94e112021-09-27 00:21:10 +02001651 const auto d = get_decorations(iid);
1652 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1653 (d.flags & decoration_set::passthrough_bit) != 0;
1654 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001655 unsigned id = insn.word(2);
1656 unsigned type = insn.word(1);
1657
ziga-lunarg9e94e112021-09-27 00:21:10 +02001658 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001659 int builtin = d.builtin;
1660 unsigned component = d.component;
1661 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1662 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001663 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001664
1665 if (builtin != -1) {
1666 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001667 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1668 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001669 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1670 // one result for each.
ziga-lunarg9e94e112021-09-27 00:21:10 +02001671 unsigned num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
sfricke-samsung962cad92021-04-13 00:46:29 -07001672 for (unsigned int offset = 0; offset < num_locations; offset++) {
1673 interface_var v = {};
1674 v.id = id;
1675 v.type_id = type;
1676 v.offset = offset;
1677 v.is_patch = is_patch;
1678 v.is_relaxed_precision = is_relaxed_precision;
1679 out[std::make_pair(location + offset, component)] = v;
1680 }
1681 }
1682 }
1683 }
1684
1685 return out;
1686}
1687
1688std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001689 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001690 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001691 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1692 auto def = get_def(id);
1693 assert(def != end());
1694 assert(def.opcode() == spv::OpVariable);
1695
1696 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1697 }
1698
1699 // Find all members belonging to the builtin block selected
1700 std::vector<uint32_t> builtin_block_members;
1701 for (auto &var : variables) {
1702 auto def = get_def(get_def(var).word(3));
1703
1704 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1705 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1706
1707 // Now find all members belonging to the struct defining the IO block
1708 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001709 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001710 auto insn = at(set.offset);
1711 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1712 // Start with undefined builtin for each struct member.
1713 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1714 if (builtin_block_members.size() == 0) {
1715 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001716 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001717 auto struct_index = insn.word(2);
1718 assert(struct_index < builtin_block_members.size());
1719 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001720 }
1721 }
1722 }
1723 }
1724
1725 return builtin_block_members;
1726}
1727
1728std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1729 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1730 std::vector<std::pair<uint32_t, interface_var>> out;
1731
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001732 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001733 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1734 auto attachment_index = insn.word(3);
1735 auto id = insn.word(1);
1736
1737 if (accessible_ids.count(id)) {
1738 auto def = get_def(id);
1739 assert(def != end());
1740 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1741 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
1742 for (unsigned int offset = 0; offset < num_locations; offset++) {
1743 interface_var v = {};
1744 v.id = id;
1745 v.type_id = def.word(1);
1746 v.offset = offset;
1747 out.emplace_back(attachment_index + offset, v);
1748 }
1749 }
1750 }
1751 }
1752 }
1753
1754 return out;
1755}
1756
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001757spirv_inst_iter SHADER_MODULE_STATE::GetImageFormatInst(uint32_t id) const
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001758{
1759 do {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001760 auto def = get_def(id);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001761 if (def == end())
1762 return def;
1763
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001764 switch (def.opcode()) {
1765 case spv::OpLoad:
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001766 case spv::OpAccessChain:
1767 case spv::OpCompositeConstruct:
1768 case spv::OpVariable: {
1769 id = def.word(1);
1770 break;
1771 }
1772
1773 case spv::OpTypeArray:
1774 case spv::OpTypeRuntimeArray:
1775 id = def.word(2);
1776 break;
1777
1778 case spv::OpTypePointer:
1779 id = def.word(3);
1780 break;
1781
1782 case spv::OpTypeImage:
1783 return def;
1784
1785 default:
1786 return end();
1787 }
1788 } while (true);
1789}
1790
ziga-lunarg8346fe82021-08-22 17:30:50 +02001791uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1792 const uint32_t opcode = iter.opcode();
1793 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1794 return 1;
1795 } else if (opcode == spv::OpTypeVector) {
1796 const uint32_t component_count = iter.word(3);
1797 return component_count;
1798 } else if (opcode == spv::OpTypeMatrix) {
1799 const auto column_type = get_def(iter.word(2));
1800 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
1801 const uint32_t column_count = iter.word(3);
1802 return vector_length * column_count;
1803 } else if (opcode == spv::OpTypeArray) {
1804 const auto element_type = get_def(iter.word(2));
1805 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1806 return element_length;
1807 } else if (opcode == spv::OpTypeStruct) {
1808 uint32_t total_size = 0;
1809 for (uint32_t i = 2; i < iter.len(); ++i) {
1810 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1811 }
1812 return total_size;
1813 } else if (opcode == spv::OpTypePointer) {
1814 const auto type = get_def(iter.word(3));
1815 return GetNumComponentsInBaseType(type);
1816 }
1817 return 0;
1818}
1819
ziga-lunarg2818f492021-08-12 14:30:51 +02001820std::array<uint32_t, 3> SHADER_MODULE_STATE::GetWorkgroupSize(
1821 VkPipelineShaderStageCreateInfo const *pStage, const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
1822 std::array<uint32_t, 3> work_group_size = {1, 1, 1};
1823
1824 uint32_t work_group_size_id = std::numeric_limits<uint32_t>::max();
1825
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001826 for (const auto &builtin : static_data_.builtin_decoration_list) {
ziga-lunarg2818f492021-08-12 14:30:51 +02001827 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1828 work_group_size_id = at(builtin.offset).word(1);
1829 break;
1830 }
1831 }
1832 for (auto insn : *this) {
1833 uint32_t opcode = insn.opcode();
1834 if (opcode == spv::OpSpecConstantComposite) { // WorkGroupSize must be a composite
1835 uint32_t result_id = insn.word(2);
1836 if (result_id == work_group_size_id) {
1837 uint32_t result_type_id = insn.word(1);
1838 auto result_type = get_def(result_type_id);
1839 if (result_type.opcode() == spv::OpTypeVector) {
1840 uint32_t component_count = result_type.word(3);
1841 for (uint32_t i = 0; i < component_count; ++i) {
1842 auto constituent = get_def(insn.word(3 + i));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001843 for (const auto &sc : static_data_.spec_const_map) {
ziga-lunarg2818f492021-08-12 14:30:51 +02001844 if (sc.second == constituent.word(2)) {
1845 const auto iter = id_value_map.find(sc.first);
1846 if (iter != id_value_map.cend()) {
1847 work_group_size[i] = *iter->second.begin();
1848 }
1849 break;
1850 }
1851 }
1852 }
1853 }
1854 }
1855 }
1856 }
1857
1858 return work_group_size;
1859}
1860
ziga-lunarga26b3602021-08-08 15:53:00 +02001861uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1862 const uint32_t opcode = iter.opcode();
1863 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1864 return iter.word(2);
1865 } else if (opcode == spv::OpTypeVector) {
1866 const auto component_type = get_def(iter.word(2));
1867 uint32_t scalar_width = GetTypeBitsSize(component_type);
1868 uint32_t component_count = iter.word(3);
1869 return scalar_width * component_count;
1870 } else if (opcode == spv::OpTypeMatrix) {
1871 const auto column_type = get_def(iter.word(2));
1872 uint32_t vector_width = GetTypeBitsSize(column_type);
1873 uint32_t column_count = iter.word(3);
1874 return vector_width * column_count;
1875 } else if (opcode == spv::OpTypeArray) {
1876 const auto element_type = get_def(iter.word(2));
1877 uint32_t element_width = GetTypeBitsSize(element_type);
1878 const auto length_type = get_def(iter.word(3));
1879 uint32_t length = GetConstantValue(length_type);
1880 return element_width * length;
1881 } else if (opcode == spv::OpTypeStruct) {
1882 uint32_t total_size = 0;
1883 for (uint32_t i = 2; i < iter.len(); ++i) {
1884 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1885 }
1886 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001887 } else if (opcode == spv::OpTypePointer) {
1888 const auto type = get_def(iter.word(3));
1889 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001890 } else if (opcode == spv::OpVariable) {
1891 const auto type = get_def(iter.word(1));
1892 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001893 }
1894 return 0;
1895}
1896
1897uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1898
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001899// 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 +02001900uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1901 const uint32_t opcode = iter.opcode();
1902 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1903 return iter.word(1);
1904 } else if (opcode == spv::OpTypeVector) {
1905 const auto& component_type = get_def(iter.word(2));
1906 return GetBaseType(component_type);
1907 } else if (opcode == spv::OpTypeMatrix) {
1908 const auto& column_type = get_def(iter.word(2));
1909 return GetBaseType(column_type);
1910 } else if (opcode == spv::OpTypeArray) {
1911 const auto& element_type = get_def(iter.word(2));
1912 return GetBaseType(element_type);
1913 } else if (opcode == spv::OpTypePointer) {
1914 const auto& type = get_def(iter.word(3));
1915 return GetBaseType(type);
1916 }
1917 return 0;
1918}
1919
ziga-lunarga26b3602021-08-08 15:53:00 +02001920uint32_t SHADER_MODULE_STATE::CalcComputeSharedMemory(VkShaderStageFlagBits stage,
1921 const spirv_inst_iter &insn) const {
1922 if (stage == VK_SHADER_STAGE_COMPUTE_BIT && insn.opcode() == spv::OpVariable) {
1923 uint32_t storage_class = insn.word(3);
1924 if (storage_class == spv::StorageClassWorkgroup) { // StorageClass Workgroup is shared memory
1925 uint32_t result_type_id = insn.word(1);
1926 auto result_type = get_def(result_type_id);
1927 auto type = get_def(result_type.word(3));
1928 return GetTypeBytesSize(type);
1929 }
1930 }
1931
1932 return 0;
1933}
1934
sfricke-samsung962cad92021-04-13 00:46:29 -07001935// Assumes itr points to an OpConstant instruction
1936uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); }
1937
1938std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1939 assert(entrypoint.opcode() == spv::OpEntryPoint);
1940
1941 std::vector<uint32_t> interfaces;
1942 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1943 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1944 uint32_t word = 3;
1945 while (entrypoint.word(word) & 0xff000000u) {
1946 ++word;
1947 }
1948 ++word;
1949
1950 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1951
1952 return interfaces;
1953}