[WIP] Refactor creation of descriptorsets

This commit is contained in:
2023-08-30 23:11:11 +02:00
parent 5aec41ead4
commit 93c75763c7
19 changed files with 201 additions and 173 deletions

View File

@@ -7,55 +7,56 @@
#pragma once
#include "Base/ICloseable.hpp"
#include "Vulkan/Scene/IRecordable.hpp"
#include "ManagedResource.hpp"
#include "Vulkan/Scene/IRecordable.hpp"
#include "Vulkan/Scene/VulkanShader.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
namespace openVulkanoCpp
namespace openVulkanoCpp::Vulkan
{
namespace Vulkan
class UniformBuffer final : IRecordable, ICloseable
{
struct UniformBuffer : virtual ICloseable, virtual IRecordable
ManagedBuffer* m_buffer;
vk::DescriptorSet m_descriptorSet;
uint32_t m_frameOffset;
public:
~UniformBuffer() override
{
ManagedBuffer* buffer;
vk::DescriptorPool descPool;
vk::DescriptorSet descSet;
vk::PipelineLayout layout;
uint32_t allocSizeFrame;
if (m_buffer) Close();
}
void Init(ManagedBuffer* buffer, uint32_t allocSizeFrame, vk::DescriptorSetLayout* descriptorSetLayout, vk::PipelineLayout layout)
{
this->buffer = buffer;
this->layout = layout;
this->allocSizeFrame = allocSizeFrame;
vk::DescriptorPoolSize poolSize = { vk::DescriptorType::eUniformBufferDynamic, 1 };
const vk::DescriptorPoolCreateInfo poolCreateInfo = { {}, 1, 1, &poolSize };
descPool = buffer->allocation->device.createDescriptorPool(poolCreateInfo);
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { descPool, 1, descriptorSetLayout };
descSet = buffer->allocation->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorBufferInfo bufferInfo = { buffer->buffer, 0, allocSizeFrame };
vk::WriteDescriptorSet writeDescriptorSet = { descSet };
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = vk::DescriptorType::eUniformBufferDynamic;
writeDescriptorSet.pBufferInfo = &bufferInfo;
buffer->allocation->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
}
void Init(ManagedBuffer* buffer, uint32_t frameOffset, uint32_t frameSize, vk::DescriptorSetLayout* descriptorSetLayout)
{
m_buffer = buffer;
m_frameOffset = frameOffset;
void Record(vk::CommandBuffer& cmdBuffer, uint32_t bufferId) override
{
uint32_t frameOffset = allocSizeFrame * bufferId;
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, layout, 0, 1,
&descSet, 1, &frameOffset);
}
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { ResourceManager::INSTANCE->descriptorPool, 1, descriptorSetLayout };
m_descriptorSet = buffer->allocation->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorBufferInfo bufferInfo = { buffer->buffer, 0, frameSize };
vk::WriteDescriptorSet writeDescriptorSet = { m_descriptorSet };
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = vk::DescriptorType::eUniformBufferDynamic;
writeDescriptorSet.pBufferInfo = &bufferInfo;
buffer->allocation->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
}
void Update(void* data, uint32_t size, uint32_t bufferId) const
{
buffer->Copy(data, size, allocSizeFrame * bufferId);
}
void Close() override
{ //TODO handle this better
m_buffer->allocation->device.freeDescriptorSets(ResourceManager::INSTANCE->descriptorPool, 1, &m_descriptorSet);
m_buffer = nullptr;
}
void Close() override
{
buffer->allocation->device.destroyDescriptorPool(descPool);
}
};
}
void Record(VulkanDrawContext* drawContext) override
{
uint32_t frameOffset = m_frameOffset * drawContext->currentImageId;
drawContext->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, drawContext->GetShader()->pipelineLayout, 0, 1,
&m_descriptorSet, 1, &frameOffset);
}
void Update(void* data, uint32_t size, uint32_t bufferId) const
{
m_buffer->Copy(data, size, m_frameOffset * bufferId);
}
};
}