Skip to content

Commit

Permalink
vulkan: use vkResetDescriptorPool
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeinikei committed Jul 30, 2023
1 parent 9d9e2da commit 8096cab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
39 changes: 19 additions & 20 deletions src/modules/graphics/vulkan/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static const TBuiltInResource defaultTBuiltInResource = {
};

static const uint32_t STREAMBUFFER_DEFAULT_SIZE = 16;
static const uint32_t DESCRIPTOR_POOL_SIZE = 16;
static const uint32_t DESCRIPTOR_POOL_SIZE = 1000;

class BindingMapper
{
Expand Down Expand Up @@ -265,10 +265,9 @@ bool Shader::loadVolatile()
createPipelineLayout();
createDescriptorPoolSizes();
createStreamBuffers();
descriptorSetsVector.resize(MAX_FRAMES_IN_FLIGHT);
descriptorPools.resize(MAX_FRAMES_IN_FLIGHT);
currentFrame = 0;
currentUsedUniformStreamBuffersCount = 0;
currentUsedDescriptorSetsCount = 0;
newFrame();

return true;
Expand Down Expand Up @@ -305,8 +304,11 @@ void Shader::unloadVolatile()
}

vgfx->queueCleanUp([shaderModules = std::move(shaderModules), device = device, descriptorSetLayout = descriptorSetLayout, pipelineLayout = pipelineLayout, descriptorPools = descriptorPools, computePipeline = computePipeline](){
for (const auto pool : descriptorPools)
vkDestroyDescriptorPool(device, pool, nullptr);
for (const auto& pools : descriptorPools)
{
for (const auto pool : pools)
vkDestroyDescriptorPool(device, pool, nullptr);
}
for (const auto shaderModule : shaderModules)
vkDestroyShaderModule(device, shaderModule, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
Expand All @@ -322,7 +324,6 @@ void Shader::unloadVolatile()
shaderStages.clear();
streamBuffers.clear();
descriptorPools.clear();
descriptorSetsVector.clear();
}

const std::vector<VkPipelineShaderStageCreateInfo> &Shader::getShaderStages() const
Expand All @@ -345,7 +346,7 @@ void Shader::newFrame()
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

currentUsedUniformStreamBuffersCount = 0;
currentUsedDescriptorSetsCount = 0;
currentDescriptorPool = 0;

if (streamBuffers.size() > 1)
{
Expand All @@ -360,14 +361,14 @@ void Shader::newFrame()
}
else
streamBuffers.at(0)->nextFrame();

for (VkDescriptorPool pool : descriptorPools[currentFrame])
vkResetDescriptorPool(device, pool, 0);
}

void Shader::cmdPushDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint bindPoint)
{
if (currentUsedDescriptorSetsCount >= static_cast<uint32_t>(descriptorSetsVector.at(currentFrame).size()))
descriptorSetsVector.at(currentFrame).push_back(allocateDescriptorSet());

VkDescriptorSet currentDescriptorSet = descriptorSetsVector.at(currentFrame).at(currentUsedDescriptorSetsCount);
VkDescriptorSet currentDescriptorSet = allocateDescriptorSet();

std::vector<VkDescriptorBufferInfo> bufferInfos{};
bufferInfos.reserve(numBuffers);
Expand Down Expand Up @@ -520,8 +521,6 @@ void Shader::cmdPushDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBind
vkUpdateDescriptorSets(device, descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);

vkCmdBindDescriptorSets(commandBuffer, bindPoint, pipelineLayout, 0, 1, &currentDescriptorSet, 0, nullptr);

currentUsedDescriptorSetsCount++;
}

Shader::~Shader()
Expand Down Expand Up @@ -597,9 +596,7 @@ void Shader::calculateUniformBufferSizeAligned()
{
auto minAlignment = vgfx->getMinUniformBufferOffsetAlignment();
size_t size = localUniformStagingData.size();
auto factor = static_cast<VkDeviceSize>(std::ceil(
static_cast<float>(size) / static_cast<float>(minAlignment)
));
auto factor = static_cast<VkDeviceSize>(std::ceil(static_cast<float>(size) / static_cast<float>(minAlignment)));
uniformBufferSizeAligned = factor * minAlignment;
}

Expand Down Expand Up @@ -1141,19 +1138,19 @@ void Shader::createDescriptorPool()
if (vkCreateDescriptorPool(device, &createInfo, nullptr, &pool) != VK_SUCCESS)
throw love::Exception("failed to create descriptor pool");

descriptorPools.push_back(pool);
descriptorPools[currentFrame].push_back(pool);
}

VkDescriptorSet Shader::allocateDescriptorSet()
{
if (descriptorPools.empty())
if (descriptorPools[currentFrame].empty())
createDescriptorPool();

while (true)
{
VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPools.back();
allocInfo.descriptorPool = descriptorPools[currentFrame][currentDescriptorPool];
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &descriptorSetLayout;

Expand All @@ -1165,7 +1162,9 @@ VkDescriptorSet Shader::allocateDescriptorSet()
case VK_SUCCESS:
return descriptorSet;
case VK_ERROR_OUT_OF_POOL_MEMORY:
createDescriptorPool();
currentDescriptorPool++;
if (descriptorPools[currentFrame].size() <= currentDescriptorPool)
createDescriptorPool();
continue;
default:
throw love::Exception("failed to allocate descriptor set");
Expand Down
5 changes: 2 additions & 3 deletions src/modules/graphics/vulkan/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ class Shader final
// we don't know how much memory we need per frame for the uniform buffer descriptors
// we keep a vector of stream buffers that gets dynamically increased if more memory is needed
std::vector<StreamBuffer*> streamBuffers;
std::vector<VkDescriptorPool> descriptorPools;
std::vector<std::vector<VkDescriptorSet>> descriptorSetsVector;
std::vector<std::vector<VkDescriptorPool>> descriptorPools;

std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
std::vector<VkShaderModule> shaderModules;
Expand All @@ -140,7 +139,7 @@ class Shader final

uint32_t currentFrame;
uint32_t currentUsedUniformStreamBuffersCount;
uint32_t currentUsedDescriptorSetsCount;
uint32_t currentDescriptorPool;
};

}
Expand Down

0 comments on commit 8096cab

Please sign in to comment.