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