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

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