• 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 "aemu/base/containers/EntityManager.h"
18 
19 #include <vulkan/vulkan.h>
20 
21 #include <unordered_set>
22 #include <vector>
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, DescriptorWriteTable& table);
109 
110 bool isDescriptorTypeImageInfo(VkDescriptorType descType);
111 bool isDescriptorTypeBufferInfo(VkDescriptorType descType);
112 bool isDescriptorTypeBufferView(VkDescriptorType descType);
113 bool isDescriptorTypeInlineUniformBlock(VkDescriptorType descType);
114 bool isDescriptorTypeAccelerationStructure(VkDescriptorType descType);
115 
116 void doEmulatedDescriptorWrite(const VkWriteDescriptorSet* write, ReifiedDescriptorSet* toWrite);
117 void doEmulatedDescriptorCopy(const VkCopyDescriptorSet* copy, const ReifiedDescriptorSet* src, ReifiedDescriptorSet* dst);
118 
119 void doEmulatedDescriptorImageInfoWriteFromTemplate(
120     VkDescriptorType descType,
121     uint32_t binding,
122     uint32_t dstArrayElement,
123     uint32_t count,
124     const VkDescriptorImageInfo* imageInfos,
125     ReifiedDescriptorSet* set);
126 
127 void doEmulatedDescriptorBufferInfoWriteFromTemplate(
128     VkDescriptorType descType,
129     uint32_t binding,
130     uint32_t dstArrayElement,
131     uint32_t count,
132     const VkDescriptorBufferInfo* bufferInfos,
133     ReifiedDescriptorSet* set);
134 
135 void doEmulatedDescriptorBufferViewWriteFromTemplate(
136     VkDescriptorType descType,
137     uint32_t binding,
138     uint32_t dstArrayElement,
139     uint32_t count,
140     const VkBufferView* bufferViews,
141     ReifiedDescriptorSet* set);
142 
143 void doEmulatedDescriptorInlineUniformBlockFromTemplate(VkDescriptorType descType, uint32_t binding,
144                                                         uint32_t dstArrayElement, uint32_t count,
145                                                         const void* pData,
146                                                         ReifiedDescriptorSet* set);
147 
148 void applyDescriptorSetAllocation(VkDescriptorPool pool, VkDescriptorSetLayout setLayout);
149 void fillDescriptorSetInfoForPool(VkDescriptorPool pool, VkDescriptorSetLayout setLayout, VkDescriptorSet set);
150 VkResult validateAndApplyVirtualDescriptorSetAllocation(const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pSets);
151 
152 // Returns false if set wasn't found in its pool.
153 bool removeDescriptorSetFromPool(VkDescriptorSet set, bool usePoolIds);
154 
155 std::vector<VkDescriptorSet> clearDescriptorPool(VkDescriptorPool pool, bool usePoolIds);
156 
157 }  // namespace vk
158 }  // namespace gfxstream
159