Repository navigation
Expand file tree
/
Copy pathBindlessManager.cpp
More file actions
203 lines (187 loc) · 8.3 KB
/
Copy pathBindlessManager.cpp
File metadata and controls
203 lines (187 loc) · 8.3 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
197
198
199
200
201
202
203
#include "BindlessManager.h"
#include "GPUContext.h"
#include "Vulkan/Helpers.h"
#include "Image.h"
#include <array>
#include "Limits.h"
namespace Magic
{
BindlessManager::BindlessManager()
{
}
BindlessManager::~BindlessManager()
{
}
void BindlessManager::Initialize(GPUContext* gpuctx)
{
m_gpuctx = gpuctx;
// Create a global descriptor pool, and let it know how many of each descriptor type we want up front
std::array<VkDescriptorPoolSize, 2> descriptorPoolSizes {{
{ VK_DESCRIPTOR_TYPE_SAMPLER, g_maxBindlessSamplerCount},
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, g_maxBindlessResourceCount}
}};
{
VkDescriptorPoolCreateInfo poolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = nullptr,
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT, // Allows us to update textures in a bindless array
.maxSets = g_maxBindlessResourceCount + g_maxBindlessSamplerCount, // ? potentially 1 set for each resource
.poolSizeCount = static_cast<uint32_t>(descriptorPoolSizes.size()),
.pPoolSizes = descriptorPoolSizes.data()
};
VK_CHECK(vkCreateDescriptorPool(m_gpuctx->GetDevice(), &poolCreateInfo, nullptr, &m_descriptorPool));
}
{
// Build a descriptor set layout
std::vector<VkDescriptorSetLayoutBinding> descriptorSetLayoutBindings;
{
uint32_t bindingIndex = 0;
for(VkDescriptorPoolSize poolSize : descriptorPoolSizes)
{
VkDescriptorSetLayoutBinding newBinding = {
.binding = bindingIndex,
.descriptorType = poolSize.type,
.descriptorCount = poolSize.descriptorCount,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.pImmutableSamplers = nullptr
};
descriptorSetLayoutBindings.push_back(newBinding);
bindingIndex++;
}
}
// Flags required for bindless stuff, need for each binding
// We only need a single layout since they are all the same for each frame in flight
const VkDescriptorBindingFlags bindlessFlags = VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT;
std::vector<VkDescriptorBindingFlags> descriptorBindingFlags;
for(size_t i = 0; i < descriptorSetLayoutBindings.size(); i++)
{
descriptorBindingFlags.push_back(bindlessFlags);
}
descriptorBindingFlags.back() |= VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT; // Permits use of variable array size for a set (with the caveat that only the last binding in the set can be of variable length)
// This is for just the texture binding only, which is hardcoded to be the last binding at the moment
{ // Finally create the layout
VkDescriptorSetLayoutBindingFlagsCreateInfoEXT extendedBindingInfo {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT,
.bindingCount = static_cast<uint32_t>(descriptorBindingFlags.size()),
.pBindingFlags = descriptorBindingFlags.data()
};
VkDescriptorSetLayoutCreateInfo setLayoutCreateInfo {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &extendedBindingInfo,
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT, // Corresponds with VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT in pool creation
.bindingCount = static_cast<uint32_t>(descriptorSetLayoutBindings.size()),
.pBindings = descriptorSetLayoutBindings.data()
};
vkCreateDescriptorSetLayout(m_gpuctx->GetDevice(), &setLayoutCreateInfo, nullptr, &m_descriptorSetLayout);
}
}
// Allocate the descriptor set
{
VkDescriptorSetVariableDescriptorCountAllocateInfoEXT variableDescriptorCountInfo { // This is specifically for the texture array binding
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT,
.descriptorSetCount = 1,
.pDescriptorCounts = &g_maxBindlessResourceCount
};
VkDescriptorSetAllocateInfo allocateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = &variableDescriptorCountInfo,
.descriptorPool = m_descriptorPool,
.descriptorSetCount = 1,
.pSetLayouts = &m_descriptorSetLayout
};
VK_CHECK(vkAllocateDescriptorSets(m_gpuctx->GetDevice(), &allocateInfo, &m_descriptorSet));
}
}
void BindlessManager::Shutdown()
{
vkDestroyDescriptorSetLayout(m_gpuctx->GetDevice(), m_descriptorSetLayout, nullptr);
vkDestroyDescriptorPool(m_gpuctx->GetDevice(), m_descriptorPool, nullptr);
}
static void UpdateBindlessTextureArrayAtIndex(const AllocatedImage &texture, uint32_t index, GPUContext* gpuctx, VkDescriptorSet descriptorSet)
{
constexpr uint32_t textureArrayBindingSlot = 1;
std::vector<VkDescriptorImageInfo> descriptorImageInfos;
{
VkDescriptorImageInfo descriptorImageInfo {
.imageView = texture.view
, .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
};
descriptorImageInfos.push_back(descriptorImageInfo);
}
std::vector<VkWriteDescriptorSet> descriptorWrites;
{
VkWriteDescriptorSet write {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
, .pNext = nullptr
, .dstSet = descriptorSet
, .dstBinding = textureArrayBindingSlot
, .dstArrayElement = index
, .descriptorCount = 1
, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
, .pImageInfo = descriptorImageInfos.data()
, .pBufferInfo = nullptr
, .pTexelBufferView = nullptr
};
descriptorWrites.push_back(write);
}
vkUpdateDescriptorSets(gpuctx->GetDevice(), static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}
bool BindlessManager::IsBindlessArrayFull()
{
std::lock_guard lock(m_bindlessMutex);
if (m_numberOfBindlessTexturesAddedSoFar == g_maxBindlessResourceCount)
{
return true;
}
return false;
}
int BindlessManager::AddToBindlessTextureArray(const AllocatedImage &texture)
{
std::lock_guard lock(m_bindlessMutex);
if (m_numberOfBindlessTexturesAddedSoFar == g_maxBindlessResourceCount)
{
Logger::Err("Ran out of bindless slots!"); // TODO:
return -1;
}
int freeIndex = m_numberOfBindlessTexturesAddedSoFar;
UpdateBindlessTextureArrayAtIndex(texture, freeIndex, m_gpuctx, m_descriptorSet);
m_numberOfBindlessTexturesAddedSoFar++;
return freeIndex;
}
void BindlessManager::UpdateBindlessSamplers(VkSampler linearSampler, VkSampler pointSampler) const
{
constexpr uint32_t bindlessSamplerBinding = 0;
VkDescriptorImageInfo linearSamplerInfo = {
.sampler = linearSampler
};
VkDescriptorImageInfo pointSamplerInfo = {
.sampler = pointSampler
};
VkWriteDescriptorSet linearSamplerDescriptorWrite = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = m_descriptorSet,
.dstBinding = bindlessSamplerBinding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &linearSamplerInfo,
.pBufferInfo = nullptr,
.pTexelBufferView = nullptr
};
VkWriteDescriptorSet pointSamplerDescriptorWrite = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = m_descriptorSet,
.dstBinding = bindlessSamplerBinding,
.dstArrayElement = 1,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &pointSamplerInfo,
.pBufferInfo = nullptr,
.pTexelBufferView = nullptr
};
std::array<VkWriteDescriptorSet, 2> samplerDescriptorWrites = { linearSamplerDescriptorWrite, pointSamplerDescriptorWrite };
vkUpdateDescriptorSets(m_gpuctx->GetDevice(), static_cast<uint32_t>(samplerDescriptorWrites.size()), samplerDescriptorWrites.data(), 0, nullptr);
}
} // namespace Magic