Added SHDebugDrawSystem class

This commit is contained in:
Kah Wei 2022-10-26 16:05:50 +08:00
parent 632df80d06
commit 550b8d85f0
9 changed files with 340 additions and 7 deletions

View File

@ -0,0 +1,155 @@
/************************************************************************************//*!
\file SHDebugDrawSystem.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 16, 2022
\brief Contains the definition of functions of the SHDebugDrawSystem class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "SHDebugDrawSystem.h"
#include "../Meshes/SHMeshData.h"
#include "../Meshes/SHPrimitiveGenerator.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "SHGraphicsSystem.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "../../SHVkUtil.h"
#include "Graphics/MiddleEnd/Interface/SHViewport.h"
#include "Graphics/MiddleEnd/Interface/SHRenderer.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* DrawRoutine */
/*---------------------------------------------------------------------------------*/
SHDebugDrawSystem::DrawRoutine::DrawRoutine()
: SHSystemRoutine("Debug Draw")
{}
void SHDebugDrawSystem::DrawRoutine::Execute(double dt) noexcept
{
auto gfxSys = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSys)
{
SHLOG_WARNING("[DebugDraw] Attempted to do debug draw without a graphics system.");
return;
}
// Create the buffer if it doesn't exist or just update it
SHDebugDrawSystem* system = static_cast<SHDebugDrawSystem*>(GetSystem());
const uint32_t DATA_SIZE = sizeof(PointVertex) * system->points.size();
SHVkUtil::EnsureBufferAndCopyHostVisibleData(gfxSys->GetDevice(), system->vertexBuffer, system->points.data(), DATA_SIZE, vk::BufferUsageFlagBits::eVertexBuffer);
// Reset for next frame
system->points.clear();
}
/*---------------------------------------------------------------------------------*/
/* SHSystem overrides */
/*---------------------------------------------------------------------------------*/
void SHDebugDrawSystem::Init()
{
// Register function for subpass
const auto* GFX_SYSTEM = SHSystemManager::GetSystem<SHGraphicsSystem>();
auto const& RENDERERS = GFX_SYSTEM->GetDefaultViewport()->GetRenderers();
auto renderGraph = RENDERERS[SHGraphicsConstants::RenderGraphIndices::WORLD]->GetRenderGraph();
auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw");
subPass->AddExteriorDrawCalls([&](Handle<SHVkCommandBuffer>& cmdBuffer)
{
cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline());
cmdBuffer->BindVertexBuffer(0, vertexBuffer, 0);
cmdBuffer->DrawArrays(static_cast<uint32_t>(points.size()), 1, 0, 0);
});
}
void SHDebugDrawSystem::Exit()
{
}
/*---------------------------------------------------------------------------------*/
/* Draw Functions */
/*---------------------------------------------------------------------------------*/
void SHDebugDrawSystem::DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt)
{
points.emplace_back(PointVertex{ startPt, color });
points.emplace_back(PointVertex{ endPt, color });
}
void SHDebugDrawSystem::DrawTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3)
{
DrawPoly(color, { pt1, pt2, pt3 });
}
void SHDebugDrawSystem::DrawQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4)
{
DrawPoly(color, { pt1, pt2, pt3, pt4 });
}
void SHDebugDrawSystem::DrawPoly(const SHVec4& color, std::initializer_list<SHVec3> pointList)
{
DrawPoly(color, pointList.begin(), pointList.end());
}
void SHDebugDrawSystem::DrawCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size)
{
static const SHVec3 EXTENTS = SHVec3 { 0.5f, 0.5f, 0.5f };
static const SHVec3 UNIT_BOT_LEFT_BACK = SHVec3 { pos - EXTENTS };
static const SHVec3 UNIT_BOT_RIGHT_BACK = SHVec3 { pos + SHVec3 { EXTENTS.x, -EXTENTS.y, 0.0f } };
static const SHVec3 UNIT_BOT_LEFT_FRONT = SHVec3 { pos + SHVec3 { 0.0f , -EXTENTS.y, EXTENTS.z } };
static const SHVec3 UNIT_BOT_RIGHT_FRONT = SHVec3 { pos + SHVec3 { EXTENTS.x, -EXTENTS.y, EXTENTS.z } };
static const SHVec3 UNIT_TOP_LEFT_BACK = SHVec3 { pos + SHVec3 { EXTENTS.x, EXTENTS.y, 0.0f } };
static const SHVec3 UNIT_TOP_RIGHT_BACK = SHVec3 { pos + SHVec3 { 0.0f , EXTENTS.y, EXTENTS.z } };
static const SHVec3 UNIT_TOP_LEFT_FRONT = SHVec3 { pos + SHVec3 { EXTENTS.x, EXTENTS.y, EXTENTS.z } };
static const SHVec3 UNIT_TOP_RIGHT_FRONT = SHVec3 { pos + EXTENTS };
const SHVec3 BOT_LEFT_BACK = UNIT_BOT_LEFT_BACK * size;
const SHVec3 BOT_RIGHT_BACK = UNIT_BOT_RIGHT_BACK * size;
const SHVec3 BOT_LEFT_FRONT = UNIT_BOT_LEFT_FRONT * size;
const SHVec3 BOT_RIGHT_FRONT = UNIT_BOT_RIGHT_FRONT * size;
const SHVec3 TOP_LEFT_BACK = UNIT_TOP_LEFT_BACK * size;
const SHVec3 TOP_RIGHT_BACK = UNIT_TOP_RIGHT_BACK * size;
const SHVec3 TOP_LEFT_FRONT = UNIT_TOP_LEFT_FRONT * size;
const SHVec3 TOP_RIGHT_FRONT = UNIT_TOP_RIGHT_FRONT * size;
DrawPoly
(
color,
{
// Bottom Square
BOT_LEFT_BACK , BOT_RIGHT_BACK,
BOT_RIGHT_BACK , BOT_RIGHT_FRONT,
BOT_RIGHT_FRONT, BOT_LEFT_FRONT,
BOT_LEFT_FRONT , BOT_LEFT_BACK,
// Middle Lines
TOP_LEFT_BACK , BOT_LEFT_BACK,
TOP_RIGHT_BACK , BOT_RIGHT_BACK,
TOP_RIGHT_FRONT, BOT_RIGHT_FRONT,
TOP_LEFT_FRONT , BOT_LEFT_FRONT,
// Top Square
TOP_LEFT_BACK , TOP_RIGHT_BACK,
TOP_RIGHT_BACK , TOP_RIGHT_FRONT,
TOP_RIGHT_FRONT, TOP_LEFT_FRONT,
TOP_LEFT_FRONT , TOP_LEFT_BACK
}
);
}
void SHDebugDrawSystem::DrawSphere(const SHVec4& color, const SHVec3& pos, double radius)
{
if (spherePoints.empty())
{
// Generate
const SHMeshData SPHERE = SHPrimitiveGenerator::Sphere();
for (const auto& idx : SPHERE.Indices)
{
spherePoints.emplace_back(SPHERE.VertexPositions[idx]);
}
}
DrawPoly(color, spherePoints.begin(), spherePoints.end());
}
}

View File

@ -0,0 +1,93 @@
/************************************************************************************//*!
\file SHDebugDrawSystem.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 16, 2022
\brief Contains the definition of the SHDebugDrawSystem class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// STL Includes
#include <vector>
// Project Includes
#include "SH_API.h"
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h"
#include "Math/Vector/SHVec4.h"
#include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h"
#include "Resource/SHHandle.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
class SHVkBuffer;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
/***********************************************************************************/
/*!
\brief
Manages the Debug Draw system.
*/
/***********************************************************************************/
class SH_API SHDebugDrawSystem : public SHSystem
{
public:
/*-------------------------------------------------------------------------------*/
/* System Routines */
/*-------------------------------------------------------------------------------*/
class SH_API DrawRoutine final : public SHSystemRoutine
{
public:
DrawRoutine();
virtual void Execute(double dt) noexcept override final;
};
/*-------------------------------------------------------------------------------*/
/* SHSystem overrides */
/*-------------------------------------------------------------------------------*/
virtual void Init() override final;
virtual void Exit() override final;
/*-------------------------------------------------------------------------------*/
/* Draw Functions */
/*-------------------------------------------------------------------------------*/
void DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt);
void DrawTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3);
void DrawQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4);
void DrawPoly(const SHVec4& color, std::initializer_list<SHVec3> pointList);
template<typename IterType>
void DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd);
void DrawCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size);
void DrawSphere(const SHVec4& color, const SHVec3& pos, double radius);
private:
/*-------------------------------------------------------------------------------*/
/* Type Definitions */
/*-------------------------------------------------------------------------------*/
struct PointVertex
{
SHVec3 Position;
SHVec4 Color;
};
/*-------------------------------------------------------------------------------*/
/* Data Members */
/*-------------------------------------------------------------------------------*/
// CPU Buffers
std::vector<PointVertex> points;
// GPU Buffers
Handle<SHVkBuffer> vertexBuffer;
// Cached Points for polygon drawing
std::vector<SHVec3> spherePoints;
};
}
#include "SHDebugDrawSystem.hpp"

View File

@ -0,0 +1,41 @@
/************************************************************************************//*!
\file SHDebugDrawSystem.hpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 16, 2022
\brief Contains the definition of template functions the SHDebugDrawSystem
class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
#include "SHDebugDrawSystem.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Template Functions */
/*---------------------------------------------------------------------------------*/
template<typename IterType>
void SHADE::SHDebugDrawSystem::DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd)
{
// Ensure dereferenced type is SHVec3
static_assert(std::is_same_v<SHVec3, std::remove_cvref_t<decltype(*pointListBegin)>>, "Parameters to DrawPoly must be SHVec3.");
const size_t POINTS_COUNT = pointListEnd - pointListBegin;
// Invalid polygon
if (POINTS_COUNT < 2)
{
SHLOG_WARNING("[SHDebugDraw] Invalid polygon provided to DrawPoly().");
return;
}
for (auto pointIter = pointListBegin + 1; pointIter != pointListEnd; ++pointIter)
{
points.emplace_back(PointVertex { *(pointIter - 1), color });
points.emplace_back(PointVertex { *pointIter , color });
}
}
}

View File

@ -127,15 +127,22 @@ namespace SHADE
shaderSourceLibrary.LoadShader(2, "KirschCs.glsl", SH_SHADER_TYPE::COMPUTE, true);
shaderSourceLibrary.LoadShader(3, "PureCopyCs.glsl", SH_SHADER_TYPE::COMPUTE, true);
shaderSourceLibrary.LoadShader(4, "DebugDrawVs.glsl", SH_SHADER_TYPE::VERTEX, true);
shaderSourceLibrary.LoadShader(5, "DebugDrawFs.glsl", SH_SHADER_TYPE::FRAGMENT, true);
shaderModuleLibrary.ImportFromSourceLibrary(device, shaderSourceLibrary);
auto cubeVS = shaderModuleLibrary.GetShaderModule("TestCubeVs.glsl");
auto cubeFS = shaderModuleLibrary.GetShaderModule("TestCubeFs.glsl");
auto greyscale = shaderModuleLibrary.GetShaderModule("KirschCs.glsl");
auto pureCopy = shaderModuleLibrary.GetShaderModule("PureCopyCs.glsl");
auto debugDrawVS = shaderModuleLibrary.GetShaderModule("TestCubeVs.glsl");
auto debugDrawFS = shaderModuleLibrary.GetShaderModule("TestCubeFs.glsl");
cubeVS->Reflect();
cubeFS->Reflect();
greyscale->Reflect();
pureCopy->Reflect();
debugDrawVS->Reflect();
debugDrawFS->Reflect();
}
void SHGraphicsSystem::InitSceneRenderGraph(void) noexcept

View File

@ -287,9 +287,8 @@ namespace SHADE
#endif
Handle<SHMousePickSystem> GetMousePickSystem(void) const noexcept {return mousePickSystem;};
Handle<SHPostOffscreenRenderSystem> GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;};
//SHRenderGraph const& GetRenderGraph(void) const noexcept;
Handle<SHVkPipeline> GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; }
//Handle<SHVkRenderpass> GetRenderPass() const { return renderPass; }
private:
@ -329,10 +328,6 @@ namespace SHADE
Handle<SHViewport> worldViewport; // Whole screen
std::vector<Handle<SHViewport>> viewports; // Additional viewports
// Debug Renderers
Handle<SHRenderer> debugWorldRenderer;
Handle<SHRenderer> debugScreenRenderer;
// Temp renderers
Handle<SHRenderer> worldRenderer;
@ -344,8 +339,9 @@ namespace SHADE
SHShaderSourceLibrary shaderSourceLibrary;
SHShaderModuleLibrary shaderModuleLibrary;
// Temp Materials
// Built-In Materials
Handle<SHMaterial> defaultMaterial;
Handle<SHVkPipeline> debugDrawPipeline;
Handle<SHRenderGraph> worldRenderGraph;

View File

@ -0,0 +1,17 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#extension GL_EXT_nonuniform_qualifier : require
layout(location = 0) in struct
{
vec4 vertColor;
} In;
layout(location = 0) out vec4 outColor;
void main()
{
outColor = In.vertColor;
}

Binary file not shown.

View File

@ -0,0 +1,24 @@
#version 450
#extension GL_KHR_vulkan_glsl : enable
layout(location = 0) in vec3 aVertexPos;
layout(location = 1) in vec4 aVertColor;
layout(location = 0) out struct
{
vec4 vertColor; // location 0
} Out;
layout(set = 2, binding = 0) uniform CameraData
{
vec4 position;
mat4 vpMat;
} cameraData;
void main()
{
gl_Position = cameraData.vpMat * vec4 (aVertexPos, 1.0f);
Out.vertColor = aVertColor;
}

Binary file not shown.