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