Renderer: Blit texture to window instead of using a full renderpass
This may improve performance and allow for render targets to customize how they blit the final texture (allowing for render-to-texture)
This commit is contained in:
@@ -230,11 +230,12 @@ namespace Nz
|
||||
PerViewerData& viewerData = *Retrieve(m_viewerData, viewer);
|
||||
|
||||
viewerData.textureArrayAttachmentIndex = frameGraph.AddAttachmentArray({
|
||||
"Directional-light cascade shadowmaps",
|
||||
m_light.GetShadowMapFormat(),
|
||||
FramePassAttachmentSize::Fixed,
|
||||
shadowMapSize, shadowMapSize,
|
||||
}, m_cascadeCount);
|
||||
.name = "Directional-light cascade shadowmaps",
|
||||
.format = m_light.GetShadowMapFormat(),
|
||||
.size = FramePassAttachmentSize::Fixed,
|
||||
.width = shadowMapSize,
|
||||
.height = shadowMapSize,
|
||||
}, SafeCast<unsigned int>(m_cascadeCount));
|
||||
|
||||
for (std::size_t i = 0; i < viewerData.cascades.size(); ++i)
|
||||
{
|
||||
|
||||
@@ -470,23 +470,6 @@ namespace Nz
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (auto&& [_, renderTargetData] : m_renderTargets)
|
||||
{
|
||||
if (renderTargetData.blitShaderBinding)
|
||||
renderFrame.PushForRelease(std::move(renderTargetData.blitShaderBinding));
|
||||
|
||||
renderTargetData.blitShaderBinding = graphics->GetBlitPipelineLayout()->AllocateShaderBinding(0);
|
||||
renderTargetData.blitShaderBinding->Update({
|
||||
{
|
||||
0,
|
||||
ShaderBinding::SampledTextureBinding {
|
||||
m_bakedFrameGraph.GetAttachmentTexture(renderTargetData.finalAttachment).get(),
|
||||
sampler.get()
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Update UBOs and materials
|
||||
@@ -511,39 +494,14 @@ namespace Nz
|
||||
m_rebuildFrameGraph = false;
|
||||
|
||||
// Final blit (TODO: Make part of frame graph)
|
||||
const Vector2ui& frameSize = renderFrame.GetSize();
|
||||
for (auto&& [renderTargetPtr, renderTargetData] : m_renderTargets)
|
||||
{
|
||||
Recti renderRegion(0, 0, frameSize.x, frameSize.y);
|
||||
|
||||
const RenderTarget& renderTarget = *renderTargetPtr;
|
||||
const auto& data = renderTargetData;
|
||||
renderFrame.Execute([&](CommandBufferBuilder& builder)
|
||||
{
|
||||
const std::shared_ptr<Texture>& sourceTexture = m_bakedFrameGraph.GetAttachmentTexture(data.finalAttachment);
|
||||
|
||||
builder.TextureBarrier(PipelineStage::ColorOutput, PipelineStage::FragmentShader, MemoryAccess::ColorWrite, MemoryAccess::ShaderRead, TextureLayout::ColorOutput, TextureLayout::ColorInput, *sourceTexture);
|
||||
|
||||
std::array<CommandBufferBuilder::ClearValues, 2> clearValues;
|
||||
clearValues[0].color = Color::Black();
|
||||
clearValues[1].depth = 1.f;
|
||||
clearValues[1].stencil = 0;
|
||||
|
||||
builder.BeginRenderPass(renderTarget.GetFramebuffer(renderFrame.GetFramebufferIndex()), renderTarget.GetRenderPass(), renderRegion, { clearValues[0], clearValues[1] });
|
||||
{
|
||||
builder.BeginDebugRegion("Main window rendering", Color::Green());
|
||||
{
|
||||
builder.SetScissor(renderRegion);
|
||||
builder.SetViewport(renderRegion);
|
||||
builder.BindRenderPipeline(*graphics->GetBlitPipeline(false));
|
||||
|
||||
builder.BindRenderShaderBinding(0, *data.blitShaderBinding);
|
||||
builder.Draw(3);
|
||||
}
|
||||
builder.EndDebugRegion();
|
||||
}
|
||||
builder.EndRenderPass();
|
||||
|
||||
renderTarget.BlitTexture(renderFrame, builder, *sourceTexture);
|
||||
}, QueueType::Graphics);
|
||||
}
|
||||
|
||||
@@ -748,13 +706,7 @@ namespace Nz
|
||||
return lhs.second->renderOrder < rhs.second->renderOrder;
|
||||
});
|
||||
|
||||
for (auto&& [_, renderTargetData] : m_renderTargets)
|
||||
{
|
||||
if (renderTargetData.blitShaderBinding)
|
||||
renderFrame.PushForRelease(std::move(renderTargetData.blitShaderBinding));
|
||||
}
|
||||
m_renderTargets.clear();
|
||||
|
||||
for (auto&& [renderTarget, viewerData] : viewers)
|
||||
{
|
||||
auto& renderTargetData = m_renderTargets[renderTarget];
|
||||
|
||||
@@ -1035,6 +1035,7 @@ namespace Nz
|
||||
data.height = attachmentData.height;
|
||||
data.size = attachmentData.size;
|
||||
data.layerCount = 1;
|
||||
data.usage = attachmentData.additionalUsage;
|
||||
|
||||
return textureId;
|
||||
}
|
||||
@@ -1077,6 +1078,7 @@ namespace Nz
|
||||
data.height = attachmentData.height;
|
||||
data.size = attachmentData.size;
|
||||
data.layerCount = attachmentData.layerCount;
|
||||
data.usage = attachmentData.additionalUsage;
|
||||
|
||||
return textureId;
|
||||
}
|
||||
@@ -1118,6 +1120,7 @@ namespace Nz
|
||||
data.height = attachmentData.height;
|
||||
data.size = attachmentData.size;
|
||||
data.layerCount = 1;
|
||||
data.usage = attachmentData.additionalUsage;
|
||||
|
||||
return textureId;
|
||||
}
|
||||
|
||||
@@ -148,10 +148,11 @@ namespace Nz
|
||||
UInt32 shadowMapSize = m_light.GetShadowMapSize();
|
||||
|
||||
m_cubeAttachmentIndex = frameGraph.AddAttachmentCube({
|
||||
"Point-light shadowmap",
|
||||
m_light.GetShadowMapFormat(),
|
||||
FramePassAttachmentSize::Fixed,
|
||||
shadowMapSize, shadowMapSize,
|
||||
.name = "Point-light shadowmap",
|
||||
.format = m_light.GetShadowMapFormat(),
|
||||
.size = FramePassAttachmentSize::Fixed,
|
||||
.width = shadowMapSize,
|
||||
.height = shadowMapSize,
|
||||
});
|
||||
|
||||
for (std::size_t i = 0; i < m_directions.size(); ++i)
|
||||
|
||||
@@ -102,10 +102,11 @@ namespace Nz
|
||||
UInt32 shadowMapSize = m_light.GetShadowMapSize();
|
||||
|
||||
m_attachmentIndex = frameGraph.AddAttachment({
|
||||
"Shadowmap",
|
||||
m_light.GetShadowMapFormat(),
|
||||
FramePassAttachmentSize::Fixed,
|
||||
shadowMapSize, shadowMapSize,
|
||||
.name = "Shadowmap",
|
||||
.format = m_light.GetShadowMapFormat(),
|
||||
.size = FramePassAttachmentSize::Fixed,
|
||||
.width = shadowMapSize,
|
||||
.height = shadowMapSize,
|
||||
});
|
||||
|
||||
FramePipelinePass::PassInputOuputs passInputOuputs;
|
||||
|
||||
@@ -146,6 +146,11 @@ namespace Nz
|
||||
context->BlitTexture(*command.source, *command.target, command.sourceBox, command.targetBox, command.filter);
|
||||
}
|
||||
|
||||
inline void OpenGLCommandBuffer::Execute(const GL::Context* context, const BlitTextureToWindowCommand& command)
|
||||
{
|
||||
context->BlitTextureToWindow(*command.source, command.sourceBox, command.targetBox, command.filter);
|
||||
}
|
||||
|
||||
inline void OpenGLCommandBuffer::Execute(const GL::Context* /*context*/, const BuildTextureMipmapsCommand& command)
|
||||
{
|
||||
command.texture->GenerateMipmaps(command.baseLevel, command.levelCount);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLSwapchain.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLUploadPool.hpp>
|
||||
#include <NazaraUtils/StackArray.hpp>
|
||||
@@ -24,101 +25,114 @@ namespace Nz
|
||||
|
||||
void OpenGLCommandBufferBuilder::BeginRenderPass(const Framebuffer& framebuffer, const RenderPass& renderPass, const Recti& /*renderRect*/, const ClearValues* clearValues, std::size_t clearValueCount)
|
||||
{
|
||||
m_commandBuffer.SetFramebuffer(static_cast<const OpenGLFramebuffer&>(framebuffer), static_cast<const OpenGLRenderPass&>(renderPass), clearValues, clearValueCount);
|
||||
m_commandBuffer.SetFramebuffer(SafeCast<const OpenGLFramebuffer&>(framebuffer), SafeCast<const OpenGLRenderPass&>(renderPass), clearValues, clearValueCount);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindComputePipeline(const ComputePipeline& pipeline)
|
||||
{
|
||||
const OpenGLComputePipeline& glPipeline = static_cast<const OpenGLComputePipeline&>(pipeline);
|
||||
const OpenGLComputePipeline& glPipeline = SafeCast<const OpenGLComputePipeline&>(pipeline);
|
||||
|
||||
m_commandBuffer.BindComputePipeline(&glPipeline);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindComputeShaderBinding(UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindComputeShaderBinding(glBinding.GetOwner(), set, &glBinding);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindComputeShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const OpenGLRenderPipelineLayout& glPipelineLayout = static_cast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
|
||||
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
const OpenGLRenderPipelineLayout& glPipelineLayout = SafeCast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
|
||||
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindComputeShaderBinding(glPipelineLayout, set, &glBinding);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindIndexBuffer(const RenderBuffer& indexBuffer, IndexType indexType, UInt64 offset)
|
||||
{
|
||||
const OpenGLBuffer& glBuffer = static_cast<const OpenGLBuffer&>(indexBuffer);
|
||||
const OpenGLBuffer& glBuffer = SafeCast<const OpenGLBuffer&>(indexBuffer);
|
||||
|
||||
m_commandBuffer.BindIndexBuffer(glBuffer.GetBuffer().GetObjectId(), indexType, offset);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindRenderPipeline(const RenderPipeline& pipeline)
|
||||
{
|
||||
const OpenGLRenderPipeline& glPipeline = static_cast<const OpenGLRenderPipeline&>(pipeline);
|
||||
const OpenGLRenderPipeline& glPipeline = SafeCast<const OpenGLRenderPipeline&>(pipeline);
|
||||
|
||||
m_commandBuffer.BindRenderPipeline(&glPipeline);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindRenderShaderBinding(UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindRenderShaderBinding(glBinding.GetOwner(), set, &glBinding);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindRenderShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const OpenGLRenderPipelineLayout& glPipelineLayout = static_cast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
|
||||
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
const OpenGLRenderPipelineLayout& glPipelineLayout = SafeCast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
|
||||
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindRenderShaderBinding(glPipelineLayout, set, &glBinding);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset)
|
||||
{
|
||||
const OpenGLBuffer& glBuffer = static_cast<const OpenGLBuffer&>(vertexBuffer);
|
||||
const OpenGLBuffer& glBuffer = SafeCast<const OpenGLBuffer&>(vertexBuffer);
|
||||
|
||||
m_commandBuffer.BindVertexBuffer(binding, glBuffer.GetBuffer().GetObjectId(), offset);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout /*fromLayout*/, const Texture& toTexture, const Boxui& toBox, TextureLayout /*toLayout*/, SamplerFilter filter)
|
||||
{
|
||||
const OpenGLTexture& sourceTexture = static_cast<const OpenGLTexture&>(fromTexture);
|
||||
const OpenGLTexture& targetTexture = static_cast<const OpenGLTexture&>(toTexture);
|
||||
const OpenGLTexture& sourceTexture = SafeCast<const OpenGLTexture&>(fromTexture);
|
||||
const OpenGLTexture& targetTexture = SafeCast<const OpenGLTexture&>(toTexture);
|
||||
|
||||
m_commandBuffer.BlitTexture(sourceTexture, fromBox, targetTexture, toBox, filter);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex)
|
||||
{
|
||||
const OpenGLTexture& glTexture = SafeCast<const OpenGLTexture&>(fromTexture);
|
||||
const OpenGLSwapchain& glSwapchain = SafeCast<const OpenGLSwapchain&>(swapchain);
|
||||
|
||||
Vector2ui swapchainSize = glSwapchain.GetSize();
|
||||
|
||||
// We set the framebuffer to ensure the correct OpenGL context is activated (in case we're using multiple contextes)
|
||||
m_commandBuffer.SetFramebuffer(glSwapchain.GetFramebuffer(imageIndex), glSwapchain.GetRenderPass(), nullptr, 0);
|
||||
|
||||
m_commandBuffer.BlitTextureToWindow(glTexture, fromBox, Boxui(0, 0, 0, swapchainSize.x, swapchainSize.y, 1), SamplerFilter::Linear);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags /*dstStageMask*/, MemoryAccessFlags /*srcAccessMask*/, MemoryAccessFlags /*dstAccessMask*/, TextureLayout /*oldLayout*/, TextureLayout /*newLayout*/)
|
||||
{
|
||||
OpenGLTexture& glTexture = static_cast<OpenGLTexture&>(texture);
|
||||
OpenGLTexture& glTexture = SafeCast<OpenGLTexture&>(texture);
|
||||
|
||||
m_commandBuffer.BuildMipmaps(glTexture, baseLevel, levelCount);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
{
|
||||
OpenGLBuffer& sourceBuffer = *static_cast<OpenGLBuffer*>(source.GetBuffer());
|
||||
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
|
||||
OpenGLBuffer& sourceBuffer = *SafeCast<OpenGLBuffer*>(source.GetBuffer());
|
||||
OpenGLBuffer& targetBuffer = *SafeCast<OpenGLBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer().GetObjectId(), targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
{
|
||||
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
|
||||
OpenGLBuffer& targetBuffer = *SafeCast<OpenGLBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(allocation, targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset, target.GetOffset() + targetOffset);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout /*fromLayout*/, const Texture& toTexture, const Vector3ui& toPos, TextureLayout /*toLayout*/)
|
||||
{
|
||||
const OpenGLTexture& sourceTexture = static_cast<const OpenGLTexture&>(fromTexture);
|
||||
const OpenGLTexture& targetTexture = static_cast<const OpenGLTexture&>(toTexture);
|
||||
const OpenGLTexture& sourceTexture = SafeCast<const OpenGLTexture&>(fromTexture);
|
||||
const OpenGLTexture& targetTexture = SafeCast<const OpenGLTexture&>(toTexture);
|
||||
|
||||
m_commandBuffer.CopyTexture(sourceTexture, fromBox, targetTexture, toPos);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLRenderImage::OpenGLRenderImage(OpenGLSwapchain& owner) :
|
||||
RenderImage(owner.GetDevice()),
|
||||
m_owner(owner),
|
||||
m_uploadPool(2 * 1024 * 1024)
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Nz
|
||||
{
|
||||
OpenGLSwapchain::OpenGLSwapchain(OpenGLDevice& device, WindowHandle windowHandle, const Vector2ui& windowSize, const SwapchainParameters& parameters) :
|
||||
m_currentFrame(0),
|
||||
m_device(device),
|
||||
m_framebuffer(*this),
|
||||
m_size(windowSize),
|
||||
m_sizeInvalidated(false)
|
||||
@@ -22,7 +23,7 @@ namespace Nz
|
||||
#endif
|
||||
//TODO: Pass swapchain parameters to context
|
||||
|
||||
m_context = device.CreateContext(contextParams, windowHandle);
|
||||
m_context = m_device.CreateContext(contextParams, windowHandle);
|
||||
if (!m_context)
|
||||
throw std::runtime_error("failed to create swapchain context");
|
||||
|
||||
|
||||
@@ -310,7 +310,7 @@ namespace Nz::GL
|
||||
}
|
||||
}
|
||||
|
||||
bool Context::BlitTexture(const OpenGLTexture& texture, const OpenGLTexture& destination, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
|
||||
bool Context::BlitTexture(const OpenGLTexture& source, const OpenGLTexture& destination, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
|
||||
{
|
||||
if (!m_blitFramebuffers && !InitializeBlitFramebuffers())
|
||||
return false;
|
||||
@@ -363,7 +363,7 @@ namespace Nz::GL
|
||||
};
|
||||
|
||||
// Attach textures to color attachment
|
||||
BindTexture(m_blitFramebuffers->readFBO, texture);
|
||||
BindTexture(m_blitFramebuffers->readFBO, source);
|
||||
BindTexture(m_blitFramebuffers->drawFBO, destination);
|
||||
|
||||
// Validate framebuffer completeness
|
||||
@@ -383,6 +383,29 @@ namespace Nz::GL
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::BlitTextureToWindow(const OpenGLTexture& texture, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
|
||||
{
|
||||
if (!m_blitFramebuffers && !InitializeBlitFramebuffers())
|
||||
return false;
|
||||
|
||||
// Bind framebuffers before configuring them (so they won't override each other)
|
||||
BindFramebuffer(FramebufferTarget::Draw, 0);
|
||||
BindFramebuffer(FramebufferTarget::Read, m_blitFramebuffers->readFBO.GetObjectId());
|
||||
|
||||
// Attach textures to color attachment
|
||||
BindTextureToFramebuffer(m_blitFramebuffers->readFBO, texture);
|
||||
|
||||
// Validate framebuffer completeness
|
||||
if (GLenum checkResult = m_blitFramebuffers->readFBO.Check(); checkResult != GL_FRAMEBUFFER_COMPLETE)
|
||||
{
|
||||
NazaraErrorFmt("blit read FBO is incomplete: {0}", TranslateOpenGLError(checkResult));
|
||||
return false;
|
||||
}
|
||||
|
||||
glBlitFramebuffer(srcBox.x, srcBox.y, srcBox.x + srcBox.width, srcBox.y + srcBox.height, dstBox.x, dstBox.y + srcBox.height, dstBox.x + dstBox.width, dstBox.y, GL_COLOR_BUFFER_BIT, ToOpenGL(filter));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::ClearErrorStack() const
|
||||
{
|
||||
assert(GetCurrentContext() == this);
|
||||
@@ -1196,4 +1219,47 @@ namespace Nz::GL
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Context::BindTextureToFramebuffer(Framebuffer& framebuffer, const OpenGLTexture& texture)
|
||||
{
|
||||
if (texture.RequiresTextureViewEmulation())
|
||||
{
|
||||
const TextureViewInfo& texViewInfo = texture.GetTextureViewInfo();
|
||||
if (texViewInfo.viewType != ImageType::E2D)
|
||||
throw std::runtime_error("unrestricted texture views can only be used as 2D texture attachment");
|
||||
|
||||
const OpenGLTexture& parentTexture = *texture.GetParentTexture();
|
||||
|
||||
switch (parentTexture.GetType())
|
||||
{
|
||||
case ImageType::Cubemap:
|
||||
{
|
||||
constexpr std::array<GLenum, 6> faceTargets = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
|
||||
assert(texViewInfo.baseArrayLayer < faceTargets.size());
|
||||
|
||||
GLenum texTarget = faceTargets[texViewInfo.baseArrayLayer];
|
||||
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, texTarget, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseMipLevel);
|
||||
break;
|
||||
}
|
||||
|
||||
case ImageType::E1D:
|
||||
case ImageType::E2D:
|
||||
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseMipLevel);
|
||||
break;
|
||||
|
||||
case ImageType::E1D_Array:
|
||||
case ImageType::E2D_Array:
|
||||
case ImageType::E3D:
|
||||
framebuffer.TextureLayer(GL_COLOR_ATTACHMENT0, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseArrayLayer, texViewInfo.baseMipLevel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (texture.GetTexture().GetTarget() != TextureTarget::Target2D)
|
||||
throw std::runtime_error("blit is not yet supported from/to other texture type than 2D textures");
|
||||
|
||||
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.GetTexture().GetObjectId(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,28 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Renderer/Swapchain.hpp>
|
||||
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
|
||||
#include <Nazara/Renderer/Texture.hpp>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
Swapchain::~Swapchain() = default;
|
||||
|
||||
void Swapchain::BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const
|
||||
{
|
||||
Vector2ui textureSize = Vector2ui(texture.GetSize());
|
||||
Boxui blitRegion(0, 0, 0, textureSize.x, textureSize.y, 1);
|
||||
|
||||
builder.TextureBarrier(PipelineStage::ColorOutput, PipelineStage::Transfer, MemoryAccess::ColorWrite, MemoryAccess::TransferRead, TextureLayout::ColorOutput, TextureLayout::TransferSource, texture);
|
||||
|
||||
builder.BeginDebugRegion("Blit to swapchain", Color::Blue());
|
||||
{
|
||||
builder.BlitTextureToSwapchain(texture, blitRegion, TextureLayout::TransferSource, *this, renderFrame.GetFramebufferIndex());
|
||||
}
|
||||
builder.EndDebugRegion();
|
||||
}
|
||||
|
||||
void Swapchain::BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies)
|
||||
{
|
||||
assert(colorFormat != PixelFormat::Undefined);
|
||||
|
||||
@@ -10,10 +10,12 @@
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanSwapchain.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanTexture.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanTextureFramebuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanUploadPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanWindowFramebuffer.hpp>
|
||||
#include <NazaraUtils/Algorithm.hpp>
|
||||
#include <NazaraUtils/StackArray.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
@@ -31,8 +33,8 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::BeginRenderPass(const Framebuffer& framebuffer, const RenderPass& renderPass, const Recti& renderRect, const ClearValues* clearValues, std::size_t clearValueCount)
|
||||
{
|
||||
const VulkanRenderPass& vkRenderPass = static_cast<const VulkanRenderPass&>(renderPass);
|
||||
const VulkanFramebuffer& vkFramebuffer = static_cast<const VulkanFramebuffer&>(framebuffer);
|
||||
const VulkanRenderPass& vkRenderPass = SafeCast<const VulkanRenderPass&>(renderPass);
|
||||
const VulkanFramebuffer& vkFramebuffer = SafeCast<const VulkanFramebuffer&>(framebuffer);
|
||||
|
||||
std::size_t attachmentCount = vkRenderPass.GetAttachmentCount();
|
||||
|
||||
@@ -80,14 +82,14 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::BindComputePipeline(const ComputePipeline& pipeline)
|
||||
{
|
||||
const VulkanComputePipeline& vkPipeline = static_cast<const VulkanComputePipeline&>(pipeline);
|
||||
const VulkanComputePipeline& vkPipeline = SafeCast<const VulkanComputePipeline&>(pipeline);
|
||||
|
||||
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_COMPUTE, vkPipeline.GetPipeline());
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BindComputeShaderBinding(UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanRenderPipelineLayout& pipelineLayout = vkBinding.GetOwner();
|
||||
|
||||
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
|
||||
@@ -95,15 +97,15 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::BindComputeShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const VulkanRenderPipelineLayout& vkPipelineLayout = static_cast<const VulkanRenderPipelineLayout&>(pipelineLayout);
|
||||
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanRenderPipelineLayout& vkPipelineLayout = SafeCast<const VulkanRenderPipelineLayout&>(pipelineLayout);
|
||||
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, vkPipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BindIndexBuffer(const RenderBuffer& indexBuffer, IndexType indexType, UInt64 offset)
|
||||
{
|
||||
const VulkanBuffer& vkBuffer = static_cast<const VulkanBuffer&>(indexBuffer);
|
||||
const VulkanBuffer& vkBuffer = SafeCast<const VulkanBuffer&>(indexBuffer);
|
||||
|
||||
m_commandBuffer.BindIndexBuffer(vkBuffer.GetBuffer(), offset, ToVulkan(indexType));
|
||||
}
|
||||
@@ -113,14 +115,14 @@ namespace Nz
|
||||
if (!m_currentRenderPass)
|
||||
throw std::runtime_error("BindPipeline must be called in a RenderPass");
|
||||
|
||||
const VulkanRenderPipeline& vkPipeline = static_cast<const VulkanRenderPipeline&>(pipeline);
|
||||
const VulkanRenderPipeline& vkPipeline = SafeCast<const VulkanRenderPipeline&>(pipeline);
|
||||
|
||||
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipeline.Get(*m_currentRenderPass, m_currentSubpassIndex));
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BindRenderShaderBinding(UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanRenderPipelineLayout& pipelineLayout = vkBinding.GetOwner();
|
||||
|
||||
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
|
||||
@@ -128,23 +130,23 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::BindRenderShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
|
||||
{
|
||||
const VulkanRenderPipelineLayout& vkPipelineLayout = static_cast<const VulkanRenderPipelineLayout&>(pipelineLayout);
|
||||
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
|
||||
const VulkanRenderPipelineLayout& vkPipelineLayout = SafeCast<const VulkanRenderPipelineLayout&>(pipelineLayout);
|
||||
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
|
||||
|
||||
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset)
|
||||
{
|
||||
const VulkanBuffer& vkBuffer = static_cast<const VulkanBuffer&>(vertexBuffer);
|
||||
const VulkanBuffer& vkBuffer = SafeCast<const VulkanBuffer&>(vertexBuffer);
|
||||
|
||||
m_commandBuffer.BindVertexBuffer(binding, vkBuffer.GetBuffer(), offset);
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter)
|
||||
{
|
||||
const VulkanTexture& vkFromTexture = static_cast<const VulkanTexture&>(fromTexture);
|
||||
const VulkanTexture& vkToTexture = static_cast<const VulkanTexture&>(toTexture);
|
||||
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
|
||||
const VulkanTexture& vkToTexture = SafeCast<const VulkanTexture&>(toTexture);
|
||||
|
||||
unsigned int fromBaseLayer, fromLayerCount;
|
||||
Image::RegionToArray(vkFromTexture.GetType(), fromBox, fromBaseLayer, fromLayerCount);
|
||||
@@ -184,9 +186,52 @@ namespace Nz
|
||||
m_commandBuffer.BlitImage(vkFromTexture.GetImage(), ToVulkan(fromLayout), vkToTexture.GetImage(), ToVulkan(toLayout), region, ToVulkan(filter));
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex)
|
||||
{
|
||||
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
|
||||
const VulkanSwapchain& vkSwapchain = SafeCast<const VulkanSwapchain&>(swapchain);
|
||||
|
||||
VkImage swapchainImage = vkSwapchain.GetImage(imageIndex);
|
||||
|
||||
VkImageSubresourceRange swapchainImageRange = {
|
||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
};
|
||||
|
||||
Boxi fromBoxInt = Boxi(fromBox);
|
||||
Vector2i swapchainSize = Vector2i(vkSwapchain.GetSize());
|
||||
|
||||
VkImageBlit swapchainBlit = {
|
||||
.srcSubresource = vkFromTexture.BuildSubresourceLayers(0),
|
||||
.srcOffsets = {
|
||||
{ fromBoxInt.x, fromBoxInt.y, fromBoxInt.z },
|
||||
{ fromBoxInt.x + fromBoxInt.width, fromBoxInt.y + fromBoxInt.height, fromBoxInt.z + fromBoxInt.depth }
|
||||
},
|
||||
.dstSubresource = {
|
||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
.mipLevel = 0,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
.dstOffsets = {
|
||||
{ 0, 0, 0 },
|
||||
{ swapchainSize.x, swapchainSize.y, 1 }
|
||||
},
|
||||
};
|
||||
|
||||
m_commandBuffer.SetImageLayout(swapchainImage, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, swapchainImageRange);
|
||||
|
||||
m_commandBuffer.BlitImage(vkFromTexture.GetImage(), ToVulkan(fromLayout), vkSwapchain.GetImage(imageIndex), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, swapchainBlit, VK_FILTER_LINEAR);
|
||||
|
||||
m_commandBuffer.SetImageLayout(swapchainImage, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, swapchainImageRange);
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout)
|
||||
{
|
||||
VulkanTexture& vkTexture = static_cast<VulkanTexture&>(texture);
|
||||
VulkanTexture& vkTexture = SafeCast<VulkanTexture&>(texture);
|
||||
VkImage vkImage = vkTexture.GetImage();
|
||||
|
||||
const TextureInfo& textureInfo = vkTexture.GetTextureInfo();
|
||||
@@ -246,24 +291,24 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
{
|
||||
VulkanBuffer& sourceBuffer = *static_cast<VulkanBuffer*>(source.GetBuffer());
|
||||
VulkanBuffer& targetBuffer = *static_cast<VulkanBuffer*>(target.GetBuffer());
|
||||
VulkanBuffer& sourceBuffer = *SafeCast<VulkanBuffer*>(source.GetBuffer());
|
||||
VulkanBuffer& targetBuffer = *SafeCast<VulkanBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer(), targetBuffer.GetBuffer(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
{
|
||||
const auto& vkAllocation = static_cast<const VulkanUploadPool::VulkanAllocation&>(allocation);
|
||||
VulkanBuffer& targetBuffer = *static_cast<VulkanBuffer*>(target.GetBuffer());
|
||||
const auto& vkAllocation = SafeCast<const VulkanUploadPool::VulkanAllocation&>(allocation);
|
||||
VulkanBuffer& targetBuffer = *SafeCast<VulkanBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(vkAllocation.buffer, targetBuffer.GetBuffer(), size, vkAllocation.offset + sourceOffset, target.GetOffset() + targetOffset);
|
||||
}
|
||||
|
||||
void VulkanCommandBufferBuilder::CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Vector3ui& toPos, TextureLayout toLayout)
|
||||
{
|
||||
const VulkanTexture& vkFromTexture = static_cast<const VulkanTexture&>(fromTexture);
|
||||
const VulkanTexture& vkToTexture = static_cast<const VulkanTexture&>(toTexture);
|
||||
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
|
||||
const VulkanTexture& vkToTexture = SafeCast<const VulkanTexture&>(toTexture);
|
||||
|
||||
unsigned int fromBaseLayer, fromLayerCount;
|
||||
Image::RegionToArray(vkFromTexture.GetType(), fromBox, fromBaseLayer, fromLayerCount);
|
||||
@@ -358,7 +403,7 @@ namespace Nz
|
||||
|
||||
void VulkanCommandBufferBuilder::TextureBarrier(PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout, const Texture& texture)
|
||||
{
|
||||
const VulkanTexture& vkTexture = static_cast<const VulkanTexture&>(texture);
|
||||
const VulkanTexture& vkTexture = SafeCast<const VulkanTexture&>(texture);
|
||||
|
||||
m_commandBuffer.ImageBarrier(ToVulkan(srcStageMask), ToVulkan(dstStageMask), VkDependencyFlags(0), ToVulkan(srcAccessMask), ToVulkan(dstAccessMask), ToVulkan(oldLayout), ToVulkan(newLayout), vkTexture.GetImage(), vkTexture.GetSubresourceRange());
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
namespace Nz
|
||||
{
|
||||
VulkanRenderImage::VulkanRenderImage(VulkanSwapchain& owner) :
|
||||
RenderImage(owner.GetDevice()),
|
||||
m_freeCommandBufferIndex(0),
|
||||
m_owner(owner),
|
||||
m_uploadPool(m_owner.GetDevice(), 2 * 1024 * 1024)
|
||||
|
||||
@@ -293,10 +293,10 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
const VulkanWindowFramebuffer& VulkanSwapchain::GetFramebuffer(std::size_t i) const
|
||||
const VulkanWindowFramebuffer& VulkanSwapchain::GetFramebuffer(std::size_t imageIndex) const
|
||||
{
|
||||
assert(i < m_framebuffers.size());
|
||||
return m_framebuffers[i];
|
||||
assert(imageIndex < m_framebuffers.size());
|
||||
return m_framebuffers[imageIndex];
|
||||
}
|
||||
|
||||
std::size_t VulkanSwapchain::GetFramebufferCount() const
|
||||
@@ -628,7 +628,7 @@ namespace Nz
|
||||
m_surfaceFormat.colorSpace,
|
||||
extent,
|
||||
1,
|
||||
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
|
||||
VK_SHARING_MODE_EXCLUSIVE,
|
||||
0, nullptr,
|
||||
surfaceCapabilities.currentTransform,
|
||||
|
||||
Reference in New Issue
Block a user