Renderer: Fix MRT support

This commit is contained in:
Jérôme Leclercq 2021-05-05 12:01:20 +02:00
parent 990193ebb4
commit 10aa7231b6
8 changed files with 74 additions and 10 deletions

View File

@ -149,6 +149,7 @@ namespace Nz
DrawStates m_currentStates;
std::size_t m_bindingIndex;
std::size_t m_maxColorBufferCount;
std::size_t m_poolIndex;
std::vector<CommandData> m_commands;
OpenGLCommandPool* m_owner;

View File

@ -11,12 +11,14 @@
namespace Nz
{
inline OpenGLCommandBuffer::OpenGLCommandBuffer() :
m_maxColorBufferCount(0),
m_owner(nullptr)
{
}
inline OpenGLCommandBuffer::OpenGLCommandBuffer(OpenGLCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex) :
m_bindingIndex(bindingIndex),
m_maxColorBufferCount(0),
m_poolIndex(poolIndex),
m_owner(&owner)
{
@ -135,6 +137,8 @@ namespace Nz
inline void OpenGLCommandBuffer::SetFramebuffer(const OpenGLFramebuffer& framebuffer, const RenderPass& /*renderPass*/, const CommandBufferBuilder::ClearValues* clearValues, std::size_t clearValueCount)
{
m_maxColorBufferCount = std::max(m_maxColorBufferCount, framebuffer.GetColorBufferCount());
SetFrameBufferData setFramebuffer;
setFramebuffer.framebuffer = &framebuffer;

View File

@ -60,6 +60,7 @@ namespace Nz
private:
Vk::CommandBuffer& m_commandBuffer;
const VulkanRenderPass* m_currentRenderPass;
std::size_t m_currentSubpassIndex;
std::size_t m_framebufferCount;
std::size_t m_imageIndex;
};

View File

@ -8,12 +8,13 @@
#define NAZARA_VULKANRENDERER_VULKANRENDERPIPELINE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Renderer/RenderPipeline.hpp>
#include <Nazara/VulkanRenderer/Config.hpp>
#include <Nazara/VulkanRenderer/VulkanRenderPass.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Device.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Pipeline.hpp>
#include <Nazara/VulkanRenderer/Wrapper/RenderPass.hpp>
#include <vector>
namespace Nz
@ -26,7 +27,7 @@ namespace Nz
VulkanRenderPipeline(Vk::Device& device, RenderPipelineInfo pipelineInfo);
~VulkanRenderPipeline() = default;
VkPipeline Get(const Vk::RenderPass& renderPass) const;
VkPipeline Get(const VulkanRenderPass& renderPass, std::size_t subpassIndex) const;
inline const RenderPipelineInfo& GetPipelineInfo() const override;
@ -71,9 +72,16 @@ namespace Nz
};
private:
mutable std::unordered_map<VkRenderPass, Vk::Pipeline> m_pipelines;
void UpdateCreateInfo(std::size_t colorBufferCount) const;
struct PipelineHasher
{
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;
MovablePtr<Vk::Device> m_device;
CreateInfo m_pipelineCreateInfo;
mutable CreateInfo m_pipelineCreateInfo;
RenderPipelineInfo m_pipelineInfo;
};
}

View File

@ -7,6 +7,15 @@
namespace Nz
{
inline std::size_t VulkanRenderPipeline::PipelineHasher::operator()(const std::pair<VkRenderPass, std::size_t>& renderPass) const
{
std::size_t seed = 0;
HashCombine(seed, renderPass.first);
HashCombine(seed, renderPass.second);
return seed;
}
inline const RenderPipelineInfo& VulkanRenderPipeline::GetPipelineInfo() const
{
return m_pipelineInfo;

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
#include <Nazara/Core/StackArray.hpp>
#include <Nazara/OpenGLRenderer/OpenGLCommandPool.hpp>
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
@ -57,6 +58,10 @@ namespace Nz
{
const GL::Context* context = GL::Context::GetCurrentContext();
StackArray<GLenum> fboDrawBuffers = NazaraStackArrayNoInit(GLenum, m_maxColorBufferCount);
for (std::size_t i = 0; i < m_maxColorBufferCount; ++i)
fboDrawBuffers[i] = GLenum(GL_COLOR_ATTACHMENT0 + i);
for (const auto& commandVariant : m_commands)
{
std::visit([&](auto&& command)
@ -103,6 +108,10 @@ namespace Nz
if (command.framebuffer->GetType() == OpenGLFramebuffer::Type::FBO)
{
std::size_t colorBufferCount = command.framebuffer->GetColorBufferCount();
assert(colorBufferCount <= fboDrawBuffers.size());
context->glDrawBuffers(GLsizei(colorBufferCount), fboDrawBuffers.data());
for (std::size_t i = 0; i < colorBufferCount; ++i)
{
Nz::Color color = command.clearValues[i].color;
@ -115,6 +124,9 @@ namespace Nz
}
else
{
GLenum buffer = GL_BACK;
context->glDrawBuffers(1, &buffer);
Nz::Color color = command.clearValues[0].color;
context->glClearColor(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
context->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -85,6 +85,7 @@ namespace Nz
m_commandBuffer.BeginRenderPass(beginInfo);
m_currentRenderPass = &vkRenderPass;
m_currentSubpassIndex = 0;
}
void VulkanCommandBufferBuilder::BindIndexBuffer(Nz::AbstractBuffer* indexBuffer, UInt64 offset)
@ -101,7 +102,7 @@ namespace Nz
const VulkanRenderPipeline& vkBinding = static_cast<const VulkanRenderPipeline&>(pipeline);
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkBinding.Get(m_currentRenderPass->GetRenderPass()));
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkBinding.Get(*m_currentRenderPass, m_currentSubpassIndex));
}
void VulkanCommandBufferBuilder::BindShaderBinding(const ShaderBinding& binding)
@ -160,6 +161,7 @@ namespace Nz
void VulkanCommandBufferBuilder::NextSubpass()
{
m_commandBuffer.NextSubpass();
m_currentSubpassIndex++;
}
void VulkanCommandBufferBuilder::PreTransferBarrier()

View File

@ -19,20 +19,31 @@ namespace Nz
m_pipelineCreateInfo = BuildCreateInfo(m_pipelineInfo);
}
VkPipeline VulkanRenderPipeline::Get(const Vk::RenderPass& renderPass) const
VkPipeline VulkanRenderPipeline::Get(const VulkanRenderPass& renderPass, std::size_t subpassIndex) const
{
if (auto it = m_pipelines.find(renderPass); it != m_pipelines.end())
const Vk::RenderPass& renderPassHandle = renderPass.GetRenderPass();
// Use color attachment count as a key
const auto& subpasses = renderPass.GetSubpassDescriptions();
assert(subpassIndex < subpasses.size());
std::size_t colorAttachmentCount = subpasses[subpassIndex].colorAttachment.size();
std::pair<VkRenderPass, std::size_t> key = { renderPassHandle, colorAttachmentCount };
if (auto it = m_pipelines.find(key); it != m_pipelines.end())
return it->second;
// Copy create info to make Get re-entrant
UpdateCreateInfo(colorAttachmentCount);
VkGraphicsPipelineCreateInfo pipelineCreateInfo = m_pipelineCreateInfo.pipelineInfo;
pipelineCreateInfo.renderPass = renderPass;
pipelineCreateInfo.renderPass = renderPassHandle;
Vk::Pipeline newPipeline;
if (!newPipeline.CreateGraphics(*m_device, pipelineCreateInfo))
return VK_NULL_HANDLE;
auto it = m_pipelines.emplace(renderPass, std::move(newPipeline)).first;
auto it = m_pipelines.emplace(key, std::move(newPipeline)).first;
return it->second;
}
@ -269,4 +280,20 @@ namespace Nz
return createInfo;
}
void VulkanRenderPipeline::UpdateCreateInfo(std::size_t colorBufferCount) const
{
// TODO: Add support for independent blend
std::size_t previousSize = m_pipelineCreateInfo.colorBlendAttachmentState.size();
if (previousSize < colorBufferCount)
{
assert(!m_pipelineCreateInfo.colorBlendAttachmentState.empty());
m_pipelineCreateInfo.colorBlendAttachmentState.resize(colorBufferCount);
for (std::size_t i = previousSize; i < colorBufferCount; ++i)
m_pipelineCreateInfo.colorBlendAttachmentState[i] = m_pipelineCreateInfo.colorBlendAttachmentState.front();
}
m_pipelineCreateInfo.stateData->colorBlendState = BuildColorBlendInfo(m_pipelineInfo, m_pipelineCreateInfo.colorBlendAttachmentState);
}
}