From 57027da80b966344746e7835219975a4e8128570 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 26 Oct 2022 16:27:58 +0800 Subject: [PATCH] Integrated into SBApplication and accounted for case where number of points to draw is 0. --- .../src/Application/SBApplication.cpp | 3 +++ .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 19 +++++++++++--- .../MiddleEnd/Interface/SHDebugDrawSystem.h | 15 +++++------ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 25 +++++++++++++++++-- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 54dc0ccf..9b230296 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -31,6 +31,7 @@ #include "FRC/SHFramerateController.h" #include "AudioSystem/SHAudioSystem.h" #include "Camera/SHCameraSystem.h" +#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" // Components #include "Graphics/MiddleEnd/Interface/SHRenderable.h" @@ -69,6 +70,7 @@ namespace Sandbox SHGraphicsSystem* graphicsSystem = static_cast(SHSystemManager::GetSystem()); SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); #ifdef SHEDITOR SDL_Init(SDL_INIT_VIDEO); @@ -90,6 +92,7 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index cd6dd7ab..7e82fb21 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -25,11 +25,11 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* DrawRoutine */ /*---------------------------------------------------------------------------------*/ - SHDebugDrawSystem::DrawRoutine::DrawRoutine() + SHDebugDrawSystem::ProcessPointsRoutine::ProcessPointsRoutine() : SHSystemRoutine("Debug Draw") {} - void SHDebugDrawSystem::DrawRoutine::Execute(double dt) noexcept + void SHDebugDrawSystem::ProcessPointsRoutine::Execute(double dt) noexcept { auto gfxSys = SHSystemManager::GetSystem(); if (gfxSys) @@ -40,8 +40,12 @@ namespace SHADE // Create the buffer if it doesn't exist or just update it SHDebugDrawSystem* system = static_cast(GetSystem()); + system->numPoints = system->points.size(); const uint32_t DATA_SIZE = sizeof(PointVertex) * system->points.size(); - SHVkUtil::EnsureBufferAndCopyHostVisibleData(gfxSys->GetDevice(), system->vertexBuffer, system->points.data(), DATA_SIZE, vk::BufferUsageFlagBits::eVertexBuffer); + if (DATA_SIZE > 0) + { + SHVkUtil::EnsureBufferAndCopyHostVisibleData(gfxSys->GetDevice(), system->vertexBuffer, system->points.data(), DATA_SIZE, vk::BufferUsageFlagBits::eVertexBuffer); + } // Reset for next frame system->points.clear(); @@ -59,6 +63,10 @@ namespace SHADE auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); subPass->AddExteriorDrawCalls([&](Handle& cmdBuffer) { + // Don't draw if no points + if (numPoints <= 0) + return; + cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); cmdBuffer->BindVertexBuffer(0, vertexBuffer, 0); cmdBuffer->DrawArrays(static_cast(points.size()), 1, 0, 0); @@ -67,7 +75,10 @@ namespace SHADE void SHDebugDrawSystem::Exit() { - + if (vertexBuffer) + { + vertexBuffer.Free(); + } } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 9344482a..cc3e728a 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -21,14 +21,10 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" #include "Resource/SHHandle.h" +#include "Graphics/Buffers/SHVkBuffer.h" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Forward Declarations */ - /*---------------------------------------------------------------------------------*/ - class SHVkBuffer; - /*---------------------------------------------------------------------------------*/ /* Type Definitions */ /*---------------------------------------------------------------------------------*/ @@ -44,10 +40,10 @@ namespace SHADE /*-------------------------------------------------------------------------------*/ /* System Routines */ /*-------------------------------------------------------------------------------*/ - class SH_API DrawRoutine final : public SHSystemRoutine - { + class SH_API ProcessPointsRoutine final : public SHSystemRoutine + { public: - DrawRoutine(); + ProcessPointsRoutine(); virtual void Execute(double dt) noexcept override final; }; @@ -73,7 +69,7 @@ namespace SHADE /*-------------------------------------------------------------------------------*/ /* Type Definitions */ /*-------------------------------------------------------------------------------*/ - struct PointVertex + struct SH_API PointVertex { SHVec3 Position; SHVec4 Color; @@ -85,6 +81,7 @@ namespace SHADE std::vector points; // GPU Buffers Handle vertexBuffer; + uint32_t numPoints = 0; // Cached Points for polygon drawing std::vector spherePoints; }; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 418e8260..6bf52736 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -204,12 +204,15 @@ namespace SHADE auto pureCopyShader = shaderModuleLibrary.GetShaderModule("PureCopyCs.glsl"); gBufferNode->AddNodeCompute(pureCopyShader, { "Scene Pre-Process", "Scene" }); + // Set up Debug Draw Pass + auto debugDrawNode = worldRenderGraph->AddNode("Debug Draw", { "Scene" }, { "G-Buffer" }); + auto debugDrawSubpass = debugDrawNode->AddSubpass("Debug Draw"); + debugDrawSubpass->AddColorOutput("Scene"); - auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, {"G-Buffer"}); // no predecessors + auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, {"Debug Draw"}); // no predecessors auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); dummySubpass->AddInput("Scene"); - // Generate world render graph worldRenderGraph->Generate(); @@ -224,6 +227,24 @@ namespace SHADE defaultMaterial = AddMaterial(cubeVS, cubeFS, gBufferSubpass); + // Create debug draw pipeline + auto debugDrawVS = shaderModuleLibrary.GetShaderModule("DebugDrawVs.glsl"); + auto debugDrawFS = shaderModuleLibrary.GetShaderModule("DebugDrawFs.glsl"); + + auto debugDrawPipelineLayout = resourceManager.Create + ( + device, SHPipelineLayoutParams + { + .shaderModules = { debugDrawVS, debugDrawFS }, + .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() + } + ); + debugDrawPipeline = resourceManager.Create(device, debugDrawPipelineLayout); + debugDrawPipeline->GetPipelineState().SetRasterizationState(SHRasterizationState + { + .polygonMode = vk::PolygonMode::eLine, + .cull_mode = vk::CullModeFlagBits::eNone + }); } void SHGraphicsSystem::InitMiddleEnd(void) noexcept