• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2021 The Android Open Source Project
2 // Copyright (C) 2021 Google Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include <unordered_set>
20 #include <vector>
21 
22 #include "aemu/base/containers/EntityManager.h"
23 
24 namespace gfxstream {
25 namespace vk {
26 
27 enum DescriptorWriteType {
28     Empty = 0,
29     ImageInfo = 1,
30     BufferInfo = 2,
31     BufferView = 3,
32     InlineUniformBlock = 4,
33     AccelerationStructure = 5,
34 };
35 
36 struct DescriptorWrite {
37     DescriptorWriteType type;
38     VkDescriptorType descriptorType;
39 
40     uint32_t dstArrayElement;  // Only used for inlineUniformBlock and accelerationStructure.
41 
42     union {
43         VkDescriptorImageInfo imageInfo;
44         VkDescriptorBufferInfo bufferInfo;
45         VkBufferView bufferView;
46         VkWriteDescriptorSetInlineUniformBlockEXT inlineUniformBlock;
47         VkWriteDescriptorSetAccelerationStructureKHR accelerationStructure;
48     };
49 
50     std::vector<uint8_t> inlineUniformBlockBuffer;
51 };
52 
53 using DescriptorWriteTable = std::vector<std::vector<DescriptorWrite>>;
54 
55 struct DescriptorWriteArrayRange {
56     uint32_t begin;
57     uint32_t count;
58 };
59 
60 using DescriptorWriteDstArrayRangeTable = std::vector<std::vector<DescriptorWriteArrayRange>>;
61 
62 struct ReifiedDescriptorSet {
63     VkDescriptorPool pool;
64     VkDescriptorSetLayout setLayout;
65     uint64_t poolId;
66     bool allocationPending;
67 
68     // Indexed first by binding number
69     DescriptorWriteTable allWrites;
70 
71     // Indexed first by binding number
72     DescriptorWriteDstArrayRangeTable pendingWriteArrayRanges;
73 
74     // Indexed by binding number
75     std::vector<bool> bindingIsImmutableSampler;
76 
77     // Copied from the descriptor set layout
78     std::vector<VkDescriptorSetLayoutBinding> bindings;
79 };
80 
81 struct DescriptorPoolAllocationInfo {
82     VkDevice device;
83     VkDescriptorPoolCreateFlags createFlags;
84 
85     // TODO: This should be in a single fancy data structure of some kind.
86     std::vector<uint64_t> freePoolIds;
87     std::unordered_set<uint32_t> allocedPoolIds;
88     std::unordered_set<VkDescriptorSet> allocedSets;
89     uint32_t maxSets;
90     uint32_t usedSets;
91 
92     // Fine-grained tracking of descriptor counts in individual pools
93     struct DescriptorCountInfo {
94         VkDescriptorType type;
95         uint32_t descriptorCount;
96         uint32_t used;
97     };
98     std::vector<DescriptorCountInfo> descriptorCountInfo;
99 };
100 
101 struct DescriptorSetLayoutInfo {
102     std::vector<VkDescriptorSetLayoutBinding> bindings;
103     uint32_t refcount;
104 };
105 
106 void clearReifiedDescriptorSet(ReifiedDescriptorSet* set);
107 
108 void initDescriptorWriteTable(const std::vector<VkDescriptorSetLayoutBinding>& layoutBindings,
109                               DescriptorWriteTable& table);
110 
111 bool isDescriptorTypeImageInfo(VkDescriptorType descType);
112 bool isDescriptorTypeBufferInfo(VkDescriptorType descType);
113 bool isDescriptorTypeBufferView(VkDescriptorType descType);
114 bool isDescriptorTypeInlineUniformBlock(VkDescriptorType descType);
115 bool isDescriptorTypeAccelerationStructure(VkDescriptorType descType);
116 
117 void doEmulatedDescriptorWrite(const VkWriteDescriptorSet* write, ReifiedDescriptorSet* toWrite);
118 void doEmulatedDescriptorCopy(const VkCopyDescriptorSet* copy, const ReifiedDescriptorSet* src,
119                               ReifiedDescriptorSet* dst);
120 
121 void doEmulatedDescriptorImageInfoWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
122                                                     uint32_t dstArrayElement, uint32_t count,
123                                                     const VkDescriptorImageInfo* imageInfos,
124                                                     ReifiedDescriptorSet* set);
125 
126 void doEmulatedDescriptorBufferInfoWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
127                                                      uint32_t dstArrayElement, uint32_t count,
128                                                      const VkDescriptorBufferInfo* bufferInfos,
129                                                      ReifiedDescriptorSet* set);
130 
131 void doEmulatedDescriptorBufferViewWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
132                                                      uint32_t dstArrayElement, uint32_t count,
133                                                      const VkBufferView* bufferViews,
134                                                      ReifiedDescriptorSet* set);
135 
136 void doEmulatedDescriptorInlineUniformBlockFromTemplate(VkDescriptorType descType, uint32_t binding,
137                                                         uint32_t dstArrayElement, uint32_t count,
138                                                         const void* pData,
139                                                         ReifiedDescriptorSet* set);
140 
141 void applyDescriptorSetAllocation(VkDescriptorPool pool, VkDescriptorSetLayout setLayout);
142 void fillDescriptorSetInfoForPool(VkDescriptorPool pool, VkDescriptorSetLayout setLayout,
143                                   VkDescriptorSet set);
144 VkResult validateAndApplyVirtualDescriptorSetAllocation(
145     const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pSets);
146 
147 // Returns false if set wasn't found in its pool.
148 bool removeDescriptorSetFromPool(VkDescriptorSet set, bool usePoolIds);
149 
150 std::vector<VkDescriptorSet> clearDescriptorPool(VkDescriptorPool pool, bool usePoolIds);
151 
152 }  // namespace vk
153 }  // namespace gfxstream
154