fixing descriptor set issue

This commit is contained in:
2025-06-01 19:57:11 +02:00
parent 195518361c
commit 724e2a277b
18 changed files with 248 additions and 67 deletions

View File

@@ -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());
}
}

View File

@@ -6,6 +6,7 @@
#include <Core/Engine.h>
#include <Platform/Window.h>
#include <Renderer/Descriptor.h>
#include <Renderer/RenderCore.h>
#include <Renderer/Pipelines/Shader.h>
#include <Renderer/Vulkan/VulkanLoader.h>
@@ -120,6 +121,8 @@ namespace Scop
m_allocator.AttachToDevice(m_device, m_physical_device);
p_descriptor_pool_manager = std::make_unique<DescriptorPoolManager>();
ShaderLayout vertex_shader_layout(
{
{ 0,
@@ -258,6 +261,8 @@ namespace Scop
if(s_instance == nullptr)
return;
WaitDeviceIdle();
p_descriptor_pool_manager->Destroy();
p_descriptor_pool_manager.reset();
m_allocator.DetachFromDevice();
for(auto shader: m_internal_shaders)
shader->Destroy();

View File

@@ -49,8 +49,8 @@ namespace Scop
};
EventBus::RegisterListener({ functor, "__ScopRender2DPass" });
p_viewer_data_set = std::make_shared<DescriptorSet>(p_vertex_shader->GetShaderLayout().set_layouts[0].second, p_vertex_shader->GetPipelineLayout().set_layouts[0], ShaderType::Vertex);
p_texture_set = std::make_shared<DescriptorSet>(p_fragment_shader->GetShaderLayout().set_layouts[0].second, p_fragment_shader->GetPipelineLayout().set_layouts[0], ShaderType::Fragment);
p_viewer_data_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_vertex_shader->GetShaderLayout().set_layouts[0].second,ShaderType::Vertex);
p_texture_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment);
p_viewer_data_buffer = std::make_shared<UniformBuffer>();
p_viewer_data_buffer->Init(sizeof(ViewerData2D));
@@ -91,7 +91,7 @@ namespace Scop
sprite_data.position = Vec2f{ static_cast<float>(sprite.GetPosition().x), static_cast<float>(sprite.GetPosition().y) };
sprite_data.color = sprite.GetColor();
if(!sprite.IsSetInit())
const_cast<Sprite&>(sprite).UpdateDescriptorSet(*p_texture_set);
const_cast<Sprite&>(sprite).UpdateDescriptorSet(p_texture_set);
const_cast<Sprite&>(sprite).Bind(frame_index, cmd);
std::array<VkDescriptorSet, 2> sets = { p_viewer_data_set->GetSet(frame_index), sprite.GetSet(frame_index) };
RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
@@ -104,7 +104,7 @@ namespace Scop
sprite_data.position = Vec2f{ static_cast<float>(text.GetPosition().x), static_cast<float>(text.GetPosition().y) };
sprite_data.color = text.GetColor();
if(!text.IsSetInit())
const_cast<Text&>(text).UpdateDescriptorSet(*p_texture_set);
const_cast<Text&>(text).UpdateDescriptorSet(p_texture_set);
const_cast<Text&>(text).Bind(frame_index, cmd);
std::array<VkDescriptorSet, 2> sets = { p_viewer_data_set->GetSet(frame_index), text.GetSet(frame_index) };
RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);

View File

@@ -31,7 +31,7 @@ namespace Scop
};
EventBus::RegisterListener({ functor, "__ScopFinalPass" });
p_set = std::make_shared<DescriptorSet>(p_fragment_shader->GetShaderLayout().set_layouts[0].second, p_fragment_shader->GetPipelineLayout().set_layouts[0], ShaderType::Fragment);
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment);
}
void FinalPass::Pass(Scene& scene, Renderer& renderer, Texture& render_target)

View File

@@ -65,7 +65,7 @@ namespace Scop
{
auto model_data = setup_model(actor);
RenderCore::Get().vkCmdPushConstants(cmd, pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ModelData), &model_data);
actor.GetModel().Draw(cmd, *data.matrices_set, pipeline, *data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex());
actor.GetModel().Draw(cmd, data.matrices_set, pipeline, data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex());
}
else
{
@@ -79,7 +79,7 @@ namespace Scop
continue;
auto model_data = setup_model(it->second);
RenderCore::Get().vkCmdPushConstants(cmd, pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ModelData), &model_data);
it->second.GetModel().Draw(cmd, *data.matrices_set, pipeline, *data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex());
it->second.GetModel().Draw(cmd, data.matrices_set, pipeline, data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex());
}
pipeline.EndPipeline(cmd);
}

View File

@@ -40,7 +40,7 @@ namespace Scop
EventBus::RegisterListener({ functor, "__ScopSkyboxPass" });
m_cube = CreateCube();
p_set = std::make_shared<DescriptorSet>(p_fragment_shader->GetShaderLayout().set_layouts[0].second, p_fragment_shader->GetPipelineLayout().set_layouts[0], ShaderType::Fragment);
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(p_fragment_shader->GetShaderLayout().set_layouts[0].second, ShaderType::Fragment);
}
void SkyboxPass::Pass(Scene& scene, Renderer& renderer, class Texture& render_target)