VulkanRenderer: Handle VulkanRenderPass destruction

This commit is contained in:
Jérôme Leclercq 2021-07-17 20:56:36 +02:00
parent 1c77a5e549
commit 40ecdda3cd
4 changed files with 33 additions and 8 deletions

View File

@ -8,6 +8,7 @@
#define NAZARA_VULKANRENDERER_VULKANRENDERPASS_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Renderer/RenderPass.hpp>
#include <Nazara/VulkanRenderer/Config.hpp>
#include <Nazara/VulkanRenderer/Wrapper/RenderPass.hpp>
@ -21,7 +22,7 @@ namespace Nz
VulkanRenderPass(Vk::Device& device, std::vector<Attachment> attachments, std::vector<SubpassDescription> subpassDescriptions, std::vector<SubpassDependency> subpassDependencies);
VulkanRenderPass(const VulkanRenderPass&) = delete;
VulkanRenderPass(VulkanRenderPass&&) noexcept = default;
~VulkanRenderPass() = default;
~VulkanRenderPass();
inline Vk::RenderPass& GetRenderPass();
inline const Vk::RenderPass& GetRenderPass() const;
@ -29,6 +30,8 @@ namespace Nz
VulkanRenderPass& operator=(const VulkanRenderPass&) = delete;
VulkanRenderPass& operator=(VulkanRenderPass&&) noexcept = default;
NazaraSignal(OnRenderPassRelease, const VulkanRenderPass* /*renderPass*/);
private:
Vk::RenderPass m_renderPass;
};

View File

@ -27,12 +27,17 @@ namespace Nz
struct CreateInfo;
VulkanRenderPipeline(VulkanDevice& device, RenderPipelineInfo pipelineInfo);
VulkanRenderPipeline(const VulkanRenderPipeline&) = delete;
VulkanRenderPipeline(VulkanRenderPipeline&&) = delete;
~VulkanRenderPipeline() = default;
VkPipeline Get(const VulkanRenderPass& renderPass, std::size_t subpassIndex) const;
inline const RenderPipelineInfo& GetPipelineInfo() const override;
VulkanRenderPipeline& operator=(const VulkanRenderPipeline&) = delete;
VulkanRenderPipeline& operator=(VulkanRenderPipeline&&) = delete;
static std::vector<VkPipelineColorBlendAttachmentState> BuildColorBlendAttachmentStateList(const RenderPipelineInfo& pipelineInfo);
static VkPipelineColorBlendStateCreateInfo BuildColorBlendInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkPipelineColorBlendAttachmentState>& attachmentState);
static VkPipelineDepthStencilStateCreateInfo BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo);
@ -81,7 +86,14 @@ namespace Nz
inline std::size_t operator()(const std::pair<VkRenderPass, std::size_t>& renderPass) const;
};
mutable std::unordered_map<std::pair<VkRenderPass, std::size_t>, Vk::Pipeline, PipelineHasher> m_pipelines;
struct PipelineData
{
NazaraSlot(VulkanRenderPass, OnRenderPassRelease, onRenderPassRelease);
Vk::Pipeline pipeline;
};
mutable std::unordered_map<std::pair<VkRenderPass, std::size_t>, PipelineData, PipelineHasher> m_pipelines;
MovablePtr<Vk::Device> m_device;
mutable CreateInfo m_pipelineCreateInfo;
RenderPipelineInfo m_pipelineInfo;

View File

@ -10,7 +10,7 @@
namespace Nz
{
inline VulkanRenderPass::VulkanRenderPass(Vk::Device& device, std::vector<Attachment> attachments, std::vector<SubpassDescription> subpassDescriptions, std::vector<SubpassDependency> subpassDependencies) :
VulkanRenderPass::VulkanRenderPass(Vk::Device& device, std::vector<Attachment> attachments, std::vector<SubpassDescription> subpassDescriptions, std::vector<SubpassDependency> subpassDependencies) :
RenderPass(std::move(attachments), std::move(subpassDescriptions), std::move(subpassDependencies))
{
std::size_t totalAttachmentReference = 0;
@ -128,4 +128,9 @@ namespace Nz
if (!m_renderPass.Create(device, renderPassInfo))
throw std::runtime_error("failed to instantiate Vulkan render pass: " + TranslateVulkanError(m_renderPass.GetLastErrorCode()));
}
VulkanRenderPass::~VulkanRenderPass()
{
OnRenderPassRelease(this);
}
}

View File

@ -35,19 +35,24 @@ namespace Nz
std::pair<VkRenderPass, std::size_t> key = { renderPassHandle, colorAttachmentCount };
if (auto it = m_pipelines.find(key); it != m_pipelines.end())
return it->second;
return it->second.pipeline;
UpdateCreateInfo(colorAttachmentCount);
VkGraphicsPipelineCreateInfo pipelineCreateInfo = m_pipelineCreateInfo.pipelineInfo;
pipelineCreateInfo.renderPass = renderPassHandle;
Vk::Pipeline newPipeline;
if (!newPipeline.CreateGraphics(*m_device, pipelineCreateInfo))
PipelineData pipelineData;
pipelineData.onRenderPassRelease.Connect(renderPass.OnRenderPassRelease, [this, key](const VulkanRenderPass*)
{
m_pipelines.erase(key);
});
if (!pipelineData.pipeline.CreateGraphics(*m_device, pipelineCreateInfo))
return VK_NULL_HANDLE;
auto it = m_pipelines.emplace(key, std::move(newPipeline)).first;
return it->second;
auto it = m_pipelines.emplace(key, std::move(pipelineData)).first;
return it->second.pipeline;
}
std::vector<VkPipelineColorBlendAttachmentState> VulkanRenderPipeline::BuildColorBlendAttachmentStateList(const RenderPipelineInfo& pipelineInfo)