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