diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp new file mode 100644 index 00000000..cd6dd7ab --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -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(); + 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(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(); + 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& cmdBuffer) + { + cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); + cmdBuffer->BindVertexBuffer(0, vertexBuffer, 0); + cmdBuffer->DrawArrays(static_cast(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 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()); + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h new file mode 100644 index 00000000..9344482a --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -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 +// 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 pointList); + template + 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 points; + // GPU Buffers + Handle vertexBuffer; + // Cached Points for polygon drawing + std::vector spherePoints; + }; +} + +#include "SHDebugDrawSystem.hpp" \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp new file mode 100644 index 00000000..90c645e3 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -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 + void SHADE::SHDebugDrawSystem::DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd) + { + // Ensure dereferenced type is SHVec3 + static_assert(std::is_same_v>, "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 }); + } + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 9b984a4b..418e8260 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -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 diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index ae93bd78..0ede74c6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -287,9 +287,8 @@ namespace SHADE #endif Handle GetMousePickSystem(void) const noexcept {return mousePickSystem;}; Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;}; - //SHRenderGraph const& GetRenderGraph(void) const noexcept; + Handle GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; } - //Handle GetRenderPass() const { return renderPass; } private: @@ -329,10 +328,6 @@ namespace SHADE Handle worldViewport; // Whole screen std::vector> viewports; // Additional viewports - // Debug Renderers - Handle debugWorldRenderer; - Handle debugScreenRenderer; - // Temp renderers Handle worldRenderer; @@ -344,8 +339,9 @@ namespace SHADE SHShaderSourceLibrary shaderSourceLibrary; SHShaderModuleLibrary shaderModuleLibrary; - // Temp Materials + // Built-In Materials Handle defaultMaterial; + Handle debugDrawPipeline; Handle worldRenderGraph; diff --git a/TempShaderFolder/DebugDrawFs.glsl b/TempShaderFolder/DebugDrawFs.glsl new file mode 100644 index 00000000..266f8ad4 --- /dev/null +++ b/TempShaderFolder/DebugDrawFs.glsl @@ -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; +} \ No newline at end of file diff --git a/TempShaderFolder/DebugDrawFs.spv b/TempShaderFolder/DebugDrawFs.spv new file mode 100644 index 00000000..dd21ce44 Binary files /dev/null and b/TempShaderFolder/DebugDrawFs.spv differ diff --git a/TempShaderFolder/DebugDrawVs.glsl b/TempShaderFolder/DebugDrawVs.glsl new file mode 100644 index 00000000..ae11658d --- /dev/null +++ b/TempShaderFolder/DebugDrawVs.glsl @@ -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; +} \ No newline at end of file diff --git a/TempShaderFolder/DebugDrawVs.spv b/TempShaderFolder/DebugDrawVs.spv new file mode 100644 index 00000000..73e83f69 Binary files /dev/null and b/TempShaderFolder/DebugDrawVs.spv differ