diff --git a/examples/GraphicsTest/main.cpp b/examples/GraphicsTest/main.cpp index 0438e964b..ee6464747 100644 --- a/examples/GraphicsTest/main.cpp +++ b/examples/GraphicsTest/main.cpp @@ -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); diff --git a/include/Nazara/Graphics/DebugDrawPipelinePass.hpp b/include/Nazara/Graphics/DebugDrawPipelinePass.hpp new file mode 100644 index 000000000..b5758060e --- /dev/null +++ b/include/Nazara/Graphics/DebugDrawPipelinePass.hpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + +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 + +#endif // NAZARA_GRAPHICS_DEBUGDRAWPIPELINEPASS_HPP diff --git a/include/Nazara/Graphics/DebugDrawPipelinePass.inl b/include/Nazara/Graphics/DebugDrawPipelinePass.inl new file mode 100644 index 000000000..f7a7a06fa --- /dev/null +++ b/include/Nazara/Graphics/DebugDrawPipelinePass.inl @@ -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 +#include + +namespace Nz +{ +} + +#include diff --git a/include/Nazara/Graphics/ForwardFramePipeline.hpp b/include/Nazara/Graphics/ForwardFramePipeline.hpp index b1871348c..f4f63f341 100644 --- a/include/Nazara/Graphics/ForwardFramePipeline.hpp +++ b/include/Nazara/Graphics/ForwardFramePipeline.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -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 depthPrepass; std::unique_ptr forwardPass; + std::unique_ptr debugDrawPass; AbstractViewer* viewer; Int32 renderOrder = 0; RenderQueueRegistry forwardRegistry; diff --git a/include/Nazara/Graphics/FramePipeline.hpp b/include/Nazara/Graphics/FramePipeline.hpp index a4fee9b93..922b20e87 100644 --- a/include/Nazara/Graphics/FramePipeline.hpp +++ b/include/Nazara/Graphics/FramePipeline.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,7 @@ namespace Nz template 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> m_elementRenderers; + DebugDrawer m_debugDrawer; }; } diff --git a/include/Nazara/Graphics/FramePipeline.inl b/include/Nazara/Graphics/FramePipeline.inl index cce001ab2..d0c2e9899 100644 --- a/include/Nazara/Graphics/FramePipeline.inl +++ b/include/Nazara/Graphics/FramePipeline.inl @@ -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()); diff --git a/include/Nazara/Graphics/Systems/RenderSystem.hpp b/include/Nazara/Graphics/Systems/RenderSystem.hpp index f2406b8b7..39c0bd7ba 100644 --- a/include/Nazara/Graphics/Systems/RenderSystem.hpp +++ b/include/Nazara/Graphics/Systems/RenderSystem.hpp @@ -40,6 +40,9 @@ namespace Nz template T& CreateWindow(Args&&... args); + inline FramePipeline& GetFramePipeline(); + inline const FramePipeline& GetFramePipeline() const; + void Update(float elapsedTime); RenderSystem& operator=(const RenderSystem&) = delete; diff --git a/include/Nazara/Graphics/Systems/RenderSystem.inl b/include/Nazara/Graphics/Systems/RenderSystem.inl index 0c8165220..23b7a6960 100644 --- a/include/Nazara/Graphics/Systems/RenderSystem.inl +++ b/include/Nazara/Graphics/Systems/RenderSystem.inl @@ -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 diff --git a/src/Nazara/Graphics/DebugDrawPipelinePass.cpp b/src/Nazara/Graphics/DebugDrawPipelinePass.cpp new file mode 100644 index 000000000..06acfe17a --- /dev/null +++ b/src/Nazara/Graphics/DebugDrawPipelinePass.cpp @@ -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 +#include +#include +#include + +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); + }); + } +} diff --git a/src/Nazara/Graphics/ForwardFramePipeline.cpp b/src/Nazara/Graphics/ForwardFramePipeline.cpp index 6bbb32956..2c87ac058 100644 --- a/src/Nazara/Graphics/ForwardFramePipeline.cpp +++ b/src/Nazara/Graphics/ForwardFramePipeline.cpp @@ -164,6 +164,7 @@ namespace Nz std::size_t viewerIndex; auto& viewerData = *m_viewerPool.Allocate(viewerIndex); viewerData.renderOrder = renderOrder; + viewerData.debugDrawPass = std::make_unique(*this, viewerInstance); viewerData.depthPrepass = std::make_unique(*this, viewerInstance); viewerData.forwardPass = std::make_unique(*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; @@ -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); diff --git a/src/Nazara/Graphics/FramePipeline.cpp b/src/Nazara/Graphics/FramePipeline.cpp index 360cdb2d7..93686ec88 100644 --- a/src/Nazara/Graphics/FramePipeline.cpp +++ b/src/Nazara/Graphics/FramePipeline.cpp @@ -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(*Graphics::Instance()->GetRenderDevice()); m_elementRenderers[UnderlyingCast(BasicRenderElement::Submesh)] = std::make_unique(); }