-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlve_descriptors.cpp
More file actions
196 lines (159 loc) · 7.27 KB
/
lve_descriptors.cpp
File metadata and controls
196 lines (159 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "lve_descriptors.hpp"
// std
#include <cassert>
#include <stdexcept>
namespace lve {
// *************** Descriptor Set Layout Builder *********************
LveDescriptorSetLayout::Builder& LveDescriptorSetLayout::Builder::addBinding(
uint32_t binding,
VkDescriptorType descriptorType,
VkShaderStageFlags stageFlags,
uint32_t count) {
assert(bindings.count(binding) == 0 && "Binding already in use");
VkDescriptorSetLayoutBinding layoutBinding{};
layoutBinding.binding = binding;
layoutBinding.descriptorType = descriptorType;
layoutBinding.descriptorCount = count;
layoutBinding.stageFlags = stageFlags;
bindings[binding] = layoutBinding;
return *this;
}
std::unique_ptr<LveDescriptorSetLayout> LveDescriptorSetLayout::Builder::build() const {
return std::make_unique<LveDescriptorSetLayout>(lveDevice, bindings);
}
// *************** Descriptor Set Layout *********************
LveDescriptorSetLayout::LveDescriptorSetLayout(
LveDevice& lveDevice, std::unordered_map<uint32_t, VkDescriptorSetLayoutBinding> bindings)
: lveDevice{ lveDevice }, bindings{ bindings } {
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings{};
for (auto kv : bindings) {
setLayoutBindings.push_back(kv.second);
}
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo{};
descriptorSetLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutInfo.bindingCount = static_cast<uint32_t>(setLayoutBindings.size());
descriptorSetLayoutInfo.pBindings = setLayoutBindings.data();
if (vkCreateDescriptorSetLayout(
lveDevice.device(),
&descriptorSetLayoutInfo,
nullptr,
&descriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create descriptor set layout!");
}
}
LveDescriptorSetLayout::~LveDescriptorSetLayout() {
vkDestroyDescriptorSetLayout(lveDevice.device(), descriptorSetLayout, nullptr);
}
// *************** Descriptor Pool Builder *********************
LveDescriptorPool::Builder& LveDescriptorPool::Builder::addPoolSize(
VkDescriptorType descriptorType, uint32_t count) {
poolSizes.push_back({ descriptorType, count });
return *this;
}
LveDescriptorPool::Builder& LveDescriptorPool::Builder::setPoolFlags(
VkDescriptorPoolCreateFlags flags) {
poolFlags = flags;
return *this;
}
LveDescriptorPool::Builder& LveDescriptorPool::Builder::setMaxSets(uint32_t count) {
maxSets = count;
return *this;
}
std::unique_ptr<LveDescriptorPool> LveDescriptorPool::Builder::build() const {
return std::make_unique<LveDescriptorPool>(lveDevice, maxSets, poolFlags, poolSizes);
}
// *************** Descriptor Pool *********************
LveDescriptorPool::LveDescriptorPool(
LveDevice& lveDevice,
uint32_t maxSets,
VkDescriptorPoolCreateFlags poolFlags,
const std::vector<VkDescriptorPoolSize>& poolSizes)
: lveDevice{ lveDevice } {
VkDescriptorPoolCreateInfo descriptorPoolInfo{};
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
descriptorPoolInfo.pPoolSizes = poolSizes.data();
descriptorPoolInfo.maxSets = maxSets;
descriptorPoolInfo.flags = poolFlags;
if (vkCreateDescriptorPool(lveDevice.device(), &descriptorPoolInfo, nullptr, &descriptorPool) !=
VK_SUCCESS) {
throw std::runtime_error("failed to create descriptor pool!");
}
}
LveDescriptorPool::~LveDescriptorPool() {
vkDestroyDescriptorPool(lveDevice.device(), descriptorPool, nullptr);
}
bool LveDescriptorPool::allocateDescriptor(
const VkDescriptorSetLayout descriptorSetLayout, VkDescriptorSet& descriptor) const {
VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.pSetLayouts = &descriptorSetLayout;
allocInfo.descriptorSetCount = 1;
// Might want to create a "DescriptorPoolManager" class that handles this case, and builds
// a new pool whenever an old pool fills up. But this is beyond our current scope
if (vkAllocateDescriptorSets(lveDevice.device(), &allocInfo, &descriptor) != VK_SUCCESS) {
return false;
}
return true;
}
void LveDescriptorPool::freeDescriptors(std::vector<VkDescriptorSet>& descriptors) const {
vkFreeDescriptorSets(
lveDevice.device(),
descriptorPool,
static_cast<uint32_t>(descriptors.size()),
descriptors.data());
}
void LveDescriptorPool::resetPool() {
vkResetDescriptorPool(lveDevice.device(), descriptorPool, 0);
}
// *************** Descriptor Writer *********************
LveDescriptorWriter::LveDescriptorWriter(LveDescriptorSetLayout& setLayout, LveDescriptorPool& pool)
: setLayout{ setLayout }, pool{ pool } {}
LveDescriptorWriter& LveDescriptorWriter::writeBuffer(
uint32_t binding, VkDescriptorBufferInfo* bufferInfo) {
assert(setLayout.bindings.count(binding) == 1 && "Layout does not contain specified binding");
auto& bindingDescription = setLayout.bindings[binding];
assert(
bindingDescription.descriptorCount == 1 &&
"Binding single descriptor info, but binding expects multiple");
VkWriteDescriptorSet write{};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write.descriptorType = bindingDescription.descriptorType;
write.dstBinding = binding;
write.pBufferInfo = bufferInfo;
write.descriptorCount = 1;
writes.push_back(write);
return *this;
}
LveDescriptorWriter& LveDescriptorWriter::writeImage(
uint32_t binding, VkDescriptorImageInfo* imageInfo) {
assert(setLayout.bindings.count(binding) == 1 && "Layout does not contain specified binding");
auto& bindingDescription = setLayout.bindings[binding];
assert(
bindingDescription.descriptorCount == 1 &&
"Binding single descriptor info, but binding expects multiple");
VkWriteDescriptorSet write{};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write.descriptorType = bindingDescription.descriptorType;
write.dstBinding = binding;
write.pImageInfo = imageInfo;
write.descriptorCount = 1;
writes.push_back(write);
return *this;
}
bool LveDescriptorWriter::build(VkDescriptorSet& set) {
bool success = pool.allocateDescriptor(setLayout.getDescriptorSetLayout(), set);
if (!success) {
return false;
}
overwrite(set);
return true;
}
void LveDescriptorWriter::overwrite(VkDescriptorSet& set) {
for (auto& write : writes) {
write.dstSet = set;
}
vkUpdateDescriptorSets(pool.lveDevice.device(), writes.size(), writes.data(), 0, nullptr);
}
} // namespace lve