mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-12 07:03:34 +00:00
fixing descriptor set issue
This commit is contained in:
@@ -5,11 +5,14 @@
|
||||
#include <Renderer/Image.h>
|
||||
#include <Renderer/Enums.h>
|
||||
#include <Renderer/Buffer.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
#include <Renderer/Descriptor.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
constexpr std::size_t MAX_SETS_PER_POOL = MAX_FRAMES_IN_FLIGHT * 1024;
|
||||
|
||||
void TransitionImageToCorrectLayout(Image& image, VkCommandBuffer cmd)
|
||||
{
|
||||
if(!image.IsInit())
|
||||
@@ -22,30 +25,144 @@ namespace Scop
|
||||
Error("Vulkan: cannot transition descriptor image layout, unkown image type");
|
||||
}
|
||||
|
||||
DescriptorSet::DescriptorSet(const ShaderSetLayout& layout, VkDescriptorSetLayout vklayout, ShaderType shader_type)
|
||||
: m_set_layout(vklayout)
|
||||
void DescriptorPool::Init() noexcept
|
||||
{
|
||||
VkDescriptorPoolSize pool_sizes[] = {
|
||||
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_SETS_PER_POOL },
|
||||
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, MAX_SETS_PER_POOL },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, MAX_SETS_PER_POOL }
|
||||
};
|
||||
|
||||
VkDescriptorPoolCreateInfo pool_info{};
|
||||
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
pool_info.poolSizeCount = sizeof(pool_sizes) / sizeof(pool_sizes[0]);
|
||||
pool_info.pPoolSizes = pool_sizes;
|
||||
pool_info.maxSets = MAX_SETS_PER_POOL;
|
||||
pool_info.flags = 0;
|
||||
kvfCheckVk(RenderCore::Get().vkCreateDescriptorPool(RenderCore::Get().GetDevice(), &pool_info, nullptr, &m_pool));
|
||||
m_allocation_count = 0;
|
||||
}
|
||||
|
||||
void DescriptorPool::Destroy() noexcept
|
||||
{
|
||||
if(m_pool == VK_NULL_HANDLE)
|
||||
return;
|
||||
for(auto& set : m_free_sets)
|
||||
kvfDestroyDescriptorSetLayout(RenderCore::Get().GetDevice(), set->m_set_layout);
|
||||
for(auto& set : m_used_sets)
|
||||
kvfDestroyDescriptorSetLayout(RenderCore::Get().GetDevice(), set->m_set_layout);
|
||||
RenderCore::Get().vkDestroyDescriptorPool(RenderCore::Get().GetDevice(), m_pool, nullptr);
|
||||
m_pool = VK_NULL_HANDLE;
|
||||
m_allocation_count = 0;
|
||||
m_free_sets.clear();
|
||||
m_used_sets.clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<DescriptorSet> DescriptorPool::RequestDescriptorSet(const ShaderSetLayout& layout, ShaderType shader_type)
|
||||
{
|
||||
auto it = std::find_if(m_free_sets.begin(), m_free_sets.end(), [&](std::shared_ptr<DescriptorSet> set)
|
||||
{
|
||||
return shader_type == set->GetShaderType() && layout == set->GetShaderLayout();
|
||||
});
|
||||
if(it != m_free_sets.end())
|
||||
{
|
||||
std::shared_ptr<DescriptorSet> set = *it;
|
||||
m_free_sets.erase(it);
|
||||
m_used_sets.push_back(set);
|
||||
return set;
|
||||
}
|
||||
|
||||
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> vulkan_sets;
|
||||
|
||||
VkShaderStageFlagBits vulkan_shader_stage;
|
||||
switch(shader_type)
|
||||
{
|
||||
case ShaderType::Vertex: vulkan_shader_stage = VK_SHADER_STAGE_VERTEX_BIT; break;
|
||||
case ShaderType::Fragment: vulkan_shader_stage = VK_SHADER_STAGE_FRAGMENT_BIT; break;
|
||||
|
||||
default: FatalError("wtf"); vulkan_shader_stage = VK_SHADER_STAGE_VERTEX_BIT; /* Just to shut up warnings */ break;
|
||||
}
|
||||
|
||||
std::vector<VkDescriptorSetLayoutBinding> bindings(layout.binds.size());
|
||||
for(std::size_t i = 0; i < layout.binds.size(); i++)
|
||||
{
|
||||
bindings[i].binding = layout.binds[i].first;
|
||||
bindings[i].descriptorCount = 1;
|
||||
bindings[i].descriptorType = layout.binds[i].second;
|
||||
bindings[i].pImmutableSamplers = nullptr;
|
||||
bindings[i].stageFlags = vulkan_shader_stage;
|
||||
}
|
||||
VkDescriptorSetLayout vulkan_layout = kvfCreateDescriptorSetLayout(RenderCore::Get().GetDevice(), bindings.data(), bindings.size());
|
||||
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
{
|
||||
VkDescriptorSetAllocateInfo alloc_info = {};
|
||||
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
alloc_info.descriptorPool = m_pool;
|
||||
alloc_info.descriptorSetCount = 1;
|
||||
alloc_info.pSetLayouts = &vulkan_layout;
|
||||
VkDescriptorSet vulkan_set;
|
||||
kvfCheckVk(RenderCore::Get().vkAllocateDescriptorSets(RenderCore::Get().GetDevice(), &alloc_info, &vulkan_set));
|
||||
m_allocation_count++;
|
||||
vulkan_sets[i] = vulkan_set;
|
||||
}
|
||||
|
||||
std::shared_ptr<DescriptorSet> set(new DescriptorSet(*this, vulkan_layout, layout, std::move(vulkan_sets), shader_type));
|
||||
m_used_sets.push_back(set);
|
||||
return set;
|
||||
}
|
||||
|
||||
void DescriptorPool::ReturnDescriptorSet(std::shared_ptr<DescriptorSet> set)
|
||||
{
|
||||
//std::size_t i = 0;
|
||||
auto it = std::find_if(m_used_sets.begin(), m_used_sets.end(), [&](const std::shared_ptr<DescriptorSet>& rhs_set)
|
||||
{
|
||||
//i++;
|
||||
//std::cout << m_used_sets.size() << " " << i << std::endl;
|
||||
return set == rhs_set;
|
||||
});
|
||||
if(it == m_used_sets.end())
|
||||
return;
|
||||
m_used_sets.erase(it);
|
||||
m_free_sets.push_back(set);
|
||||
}
|
||||
|
||||
DescriptorPool& DescriptorPoolManager::GetAvailablePool()
|
||||
{
|
||||
for(auto& pool : m_pools)
|
||||
{
|
||||
if(pool.GetNumberOfSetsAllocated() < MAX_SETS_PER_POOL)
|
||||
return pool;
|
||||
}
|
||||
m_pools.emplace_back().Init();
|
||||
return m_pools.back();
|
||||
}
|
||||
|
||||
void DescriptorPoolManager::Destroy()
|
||||
{
|
||||
for(auto& pool : m_pools)
|
||||
pool.Destroy();
|
||||
m_pools.clear();
|
||||
}
|
||||
|
||||
DescriptorSet::DescriptorSet(DescriptorPool& pool, VkDescriptorSetLayout vulkan_layout, const ShaderSetLayout& layout, std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> vulkan_sets, ShaderType shader_type) :
|
||||
m_shader_layout(layout),
|
||||
m_sets(std::move(vulkan_sets)),
|
||||
m_set_layout(vulkan_layout),
|
||||
m_shader_type(shader_type),
|
||||
m_pool(pool)
|
||||
{
|
||||
for(auto& [binding, type] : layout.binds)
|
||||
{
|
||||
m_descriptors.emplace_back();
|
||||
m_descriptors.back().type = type;
|
||||
m_descriptors.back().shader_type = shader_type;
|
||||
m_descriptors.back().binding = binding;
|
||||
}
|
||||
for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
m_set[i] = kvfAllocateDescriptorSet(RenderCore::Get().GetDevice(), vklayout);
|
||||
}
|
||||
|
||||
DescriptorSet::DescriptorSet(VkDescriptorSetLayout layout, const std::vector<Descriptor>& descriptors)
|
||||
: m_set_layout(layout), m_descriptors(descriptors)
|
||||
{
|
||||
for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
m_set[i] = kvfAllocateDescriptorSet(RenderCore::Get().GetDevice(), layout);
|
||||
}
|
||||
|
||||
void DescriptorSet::SetImage(std::size_t i, std::uint32_t binding, class Image& image)
|
||||
{
|
||||
Verify(m_set[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
Verify(m_sets[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
auto it = std::find_if(m_descriptors.begin(), m_descriptors.end(), [=](Descriptor descriptor)
|
||||
{
|
||||
return binding == descriptor.binding;
|
||||
@@ -65,7 +182,7 @@ namespace Scop
|
||||
|
||||
void DescriptorSet::SetStorageBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer)
|
||||
{
|
||||
Verify(m_set[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
Verify(m_sets[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
auto it = std::find_if(m_descriptors.begin(), m_descriptors.end(), [=](Descriptor descriptor)
|
||||
{
|
||||
return binding == descriptor.binding;
|
||||
@@ -85,7 +202,7 @@ namespace Scop
|
||||
|
||||
void DescriptorSet::SetUniformBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer)
|
||||
{
|
||||
Verify(m_set[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
Verify(m_sets[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
auto it = std::find_if(m_descriptors.begin(), m_descriptors.end(), [=](Descriptor descriptor)
|
||||
{
|
||||
return binding == descriptor.binding;
|
||||
@@ -105,7 +222,7 @@ namespace Scop
|
||||
|
||||
void DescriptorSet::Update(std::size_t i, VkCommandBuffer cmd) noexcept
|
||||
{
|
||||
Verify(m_set[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
Verify(m_sets[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
|
||||
std::size_t image_count = 0;
|
||||
std::size_t buffer_count = 0;
|
||||
@@ -137,7 +254,7 @@ namespace Scop
|
||||
info.imageLayout = descriptor.image_ptr->GetLayout();
|
||||
info.imageView = descriptor.image_ptr->GetImageView();
|
||||
image_infos[image_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteImageToDescriptorSet(RenderCore::Get().GetDevice(), m_set[i], &image_infos[image_index], descriptor.binding);
|
||||
writes[write_index] = kvfWriteImageToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &image_infos[image_index], descriptor.binding);
|
||||
image_index++;
|
||||
}
|
||||
else if(descriptor.uniform_buffer_ptr)
|
||||
@@ -147,7 +264,7 @@ namespace Scop
|
||||
info.offset = descriptor.uniform_buffer_ptr->GetOffset();
|
||||
info.range = VK_WHOLE_SIZE;
|
||||
buffer_infos[buffer_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteUniformBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_set[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
writes[write_index] = kvfWriteUniformBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
buffer_index++;
|
||||
}
|
||||
else if(descriptor.storage_buffer_ptr)
|
||||
@@ -157,11 +274,16 @@ namespace Scop
|
||||
info.offset = descriptor.storage_buffer_ptr->GetOffset();
|
||||
info.range = VK_WHOLE_SIZE;
|
||||
buffer_infos[buffer_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteStorageBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_set[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
writes[write_index] = kvfWriteStorageBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
buffer_index++;
|
||||
}
|
||||
write_index++;
|
||||
}
|
||||
RenderCore::Get().vkUpdateDescriptorSets(RenderCore::Get().GetDevice(), writes.size(), writes.data(), 0, nullptr);
|
||||
}
|
||||
|
||||
void DescriptorSet::ReturnDescriptorSetToPool()
|
||||
{
|
||||
m_pool.ReturnDescriptorSet(shared_from_this());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user