Graphics: Add DebugDrawer support

This commit is contained in:
SirLynix 2022-08-17 20:12:49 +02:00
parent 4a5f866754
commit f1549b934c
11 changed files with 156 additions and 9 deletions

View File

@ -220,6 +220,8 @@ int main()
continue;
}
framePipeline.GetDebugDrawer().DrawLine(Nz::Vector3f::Zero(), Nz::Vector3f::Forward(), Nz::Color::Blue);
viewerInstance.UpdateViewMatrix(Nz::Matrix4f::TransformInverse(viewerPos, camAngles));
viewerInstance.UpdateEyePosition(viewerPos);

View File

@ -0,0 +1,50 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_GRAPHICS_DEBUGDRAWPIPELINEPASS_HPP
#define NAZARA_GRAPHICS_DEBUGDRAWPIPELINEPASS_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/ElementRenderer.hpp>
#include <Nazara/Graphics/FramePipelinePass.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/RenderElement.hpp>
#include <Nazara/Graphics/RenderQueue.hpp>
#include <Nazara/Graphics/RenderQueueRegistry.hpp>
#include <Nazara/Math/Frustum.hpp>
namespace Nz
{
class AbstractViewer;
class FrameGraph;
class FramePipeline;
class Material;
class NAZARA_GRAPHICS_API DebugDrawPipelinePass : public FramePipelinePass
{
public:
DebugDrawPipelinePass(FramePipeline& owner, AbstractViewer* viewer);
DebugDrawPipelinePass(const DebugDrawPipelinePass&) = delete;
DebugDrawPipelinePass(DebugDrawPipelinePass&&) = delete;
~DebugDrawPipelinePass() = default;
void Prepare(RenderFrame& renderFrame);
void RegisterToFrameGraph(FrameGraph& frameGraph, std::size_t inputColorBufferIndex, std::size_t outputColorBufferIndex);
DepthPipelinePass& operator=(const DepthPipelinePass&) = delete;
DepthPipelinePass& operator=(DepthPipelinePass&&) = delete;
private:
AbstractViewer* m_viewer;
FramePipeline& m_pipeline;
};
}
#include <Nazara/Graphics/DebugDrawPipelinePass.inl>
#endif // NAZARA_GRAPHICS_DEBUGDRAWPIPELINEPASS_HPP

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/DebugDrawPipelinePass.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -11,6 +11,7 @@
#include <Nazara/Graphics/BakedFrameGraph.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/DepthPipelinePass.hpp>
#include <Nazara/Graphics/DebugDrawPipelinePass.hpp>
#include <Nazara/Graphics/ElementRenderer.hpp>
#include <Nazara/Graphics/ForwardPipelinePass.hpp>
#include <Nazara/Graphics/FramePipeline.hpp>
@ -106,10 +107,12 @@ namespace Nz
struct ViewerData
{
std::size_t colorAttachment;
std::size_t forwardColorAttachment;
std::size_t debugColorAttachment;
std::size_t depthStencilAttachment;
std::unique_ptr<DepthPipelinePass> depthPrepass;
std::unique_ptr<ForwardPipelinePass> forwardPass;
std::unique_ptr<DebugDrawPipelinePass> debugDrawPass;
AbstractViewer* viewer;
Int32 renderOrder = 0;
RenderQueueRegistry forwardRegistry;

View File

@ -12,6 +12,7 @@
#include <Nazara/Graphics/RenderElement.hpp>
#include <Nazara/Graphics/RenderQueue.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Renderer/DebugDrawer.hpp>
#include <memory>
#include <vector>
@ -34,6 +35,7 @@ namespace Nz
template<typename F> void ForEachElementRenderer(F&& callback);
inline DebugDrawer& GetDebugDrawer();
inline ElementRenderer& GetElementRenderer(std::size_t elementIndex);
inline std::size_t GetElementRendererCount() const;
@ -66,6 +68,7 @@ namespace Nz
private:
std::vector<std::unique_ptr<ElementRenderer>> m_elementRenderers;
DebugDrawer m_debugDrawer;
};
}

View File

@ -8,6 +8,11 @@
namespace Nz
{
inline DebugDrawer& FramePipeline::GetDebugDrawer()
{
return m_debugDrawer;
}
inline ElementRenderer& FramePipeline::GetElementRenderer(std::size_t elementIndex)
{
assert(elementIndex < m_elementRenderers.size());

View File

@ -40,6 +40,9 @@ namespace Nz
template<typename T = RenderWindow, typename... Args> T& CreateWindow(Args&&... args);
inline FramePipeline& GetFramePipeline();
inline const FramePipeline& GetFramePipeline() const;
void Update(float elapsedTime);
RenderSystem& operator=(const RenderSystem&) = delete;

View File

@ -20,6 +20,16 @@ namespace Nz
return windowRef;
}
inline FramePipeline& RenderSystem::GetFramePipeline()
{
return *m_pipeline;
}
inline const FramePipeline& RenderSystem::GetFramePipeline() const
{
return *m_pipeline;
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -0,0 +1,47 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/DebugDrawPipelinePass.hpp>
#include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Renderer/RenderFrame.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
DebugDrawPipelinePass::DebugDrawPipelinePass(FramePipeline& owner, AbstractViewer* viewer) :
m_viewer(viewer),
m_pipeline(owner)
{
}
void DebugDrawPipelinePass::Prepare(RenderFrame& renderFrame)
{
DebugDrawer& debugDrawer = m_pipeline.GetDebugDrawer();
debugDrawer.SetViewerData(m_viewer->GetViewerInstance().GetViewProjMatrix());
debugDrawer.Prepare(renderFrame);
}
void DebugDrawPipelinePass::RegisterToFrameGraph(FrameGraph& frameGraph, std::size_t inputColorBufferIndex, std::size_t outputColorBufferIndex)
{
FramePass& debugDrawPass = frameGraph.AddPass("Debug draw pass");
debugDrawPass.AddInput(inputColorBufferIndex);
debugDrawPass.AddOutput(outputColorBufferIndex);
debugDrawPass.SetExecutionCallback([&]()
{
return FramePassExecution::UpdateAndExecute;
});
debugDrawPass.SetCommandCallback([this](CommandBufferBuilder& builder, const FramePassEnvironment& /*env*/)
{
Recti viewport = m_viewer->GetViewport();
builder.SetScissor(viewport);
builder.SetViewport(viewport);
DebugDrawer& debugDrawer = m_pipeline.GetDebugDrawer();
debugDrawer.Draw(builder);
});
}
}

View File

@ -164,6 +164,7 @@ namespace Nz
std::size_t viewerIndex;
auto& viewerData = *m_viewerPool.Allocate(viewerIndex);
viewerData.renderOrder = renderOrder;
viewerData.debugDrawPass = std::make_unique<DebugDrawPipelinePass>(*this, viewerInstance);
viewerData.depthPrepass = std::make_unique<DepthPipelinePass>(*this, viewerInstance);
viewerData.forwardPass = std::make_unique<ForwardPipelinePass>(*this, viewerInstance);
viewerData.viewer = viewerInstance;
@ -209,7 +210,7 @@ namespace Nz
renderFrame.Execute([&](CommandBufferBuilder& builder)
{
builder.BeginDebugRegion("UBO Update", Color::Yellow);
builder.BeginDebugRegion("CPU to GPU transfers", Color::Yellow);
{
builder.PreTransferBarrier();
@ -297,6 +298,8 @@ namespace Nz
viewerData.depthPrepass->Prepare(renderFrame, frustum, m_visibleRenderables, depthVisibilityHash);
viewerData.forwardPass->Prepare(renderFrame, frustum, m_visibleRenderables, m_visibleLights, visibilityHash);
viewerData.debugDrawPass->Prepare(renderFrame);
}
if (m_bakedFrameGraph.Resize(renderFrame))
@ -312,7 +315,7 @@ namespace Nz
{
0,
ShaderBinding::TextureBinding {
m_bakedFrameGraph.GetAttachmentTexture(viewerData.colorAttachment).get(),
m_bakedFrameGraph.GetAttachmentTexture(viewerData.debugColorAttachment).get(),
sampler.get()
}
}
@ -376,6 +379,10 @@ namespace Nz
}, QueueType::Graphics);
}
// reset at the end instead of the beginning so debug draw can be used before calling this method
DebugDrawer& debugDrawer = GetDebugDrawer();
debugDrawer.Reset(renderFrame);
}
void ForwardFramePipeline::UnregisterLight(std::size_t lightIndex)
@ -475,10 +482,12 @@ namespace Nz
for (auto& viewerData : m_viewerPool)
{
viewerData.colorAttachment = frameGraph.AddAttachment({
"Color",
viewerData.forwardColorAttachment = frameGraph.AddAttachment({
"Forward output",
PixelFormat::RGBA8
});
viewerData.debugColorAttachment = frameGraph.AddAttachmentProxy("Debug draw output", viewerData.forwardColorAttachment);
viewerData.depthStencilAttachment = frameGraph.AddAttachment({
"Depth-stencil buffer",
@ -488,7 +497,9 @@ namespace Nz
if (viewerData.depthPrepass)
viewerData.depthPrepass->RegisterToFrameGraph(frameGraph, viewerData.depthStencilAttachment);
viewerData.forwardPass->RegisterToFrameGraph(frameGraph, viewerData.colorAttachment, viewerData.depthStencilAttachment, viewerData.depthPrepass != nullptr);
viewerData.forwardPass->RegisterToFrameGraph(frameGraph, viewerData.forwardColorAttachment, viewerData.depthStencilAttachment, viewerData.depthPrepass != nullptr);
viewerData.debugDrawPass->RegisterToFrameGraph(frameGraph, viewerData.forwardColorAttachment, viewerData.debugColorAttachment);
}
using ViewerPair = std::pair<const RenderTarget*, const ViewerData*>;
@ -526,7 +537,7 @@ namespace Nz
});
for (const ViewerData* viewerData : targetViewers)
mergePass.AddInput(viewerData->colorAttachment);
mergePass.AddInput(viewerData->debugColorAttachment);
mergePass.AddOutput(renderTargetData.finalAttachment);
mergePass.SetClearColor(0, Color::Black);

View File

@ -11,9 +11,10 @@
namespace Nz
{
FramePipeline::FramePipeline()
FramePipeline::FramePipeline() :
m_elementRenderers(BasicRenderElementCount),
m_debugDrawer(*Graphics::Instance()->GetRenderDevice())
{
m_elementRenderers.resize(BasicRenderElementCount);
m_elementRenderers[UnderlyingCast(BasicRenderElement::SpriteChain)] = std::make_unique<SpriteChainRenderer>(*Graphics::Instance()->GetRenderDevice());
m_elementRenderers[UnderlyingCast(BasicRenderElement::Submesh)] = std::make_unique<SubmeshRenderer>();
}