From 632df80d068e7cd1049ae3423a254cc1c3f4413e Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 26 Oct 2022 16:05:18 +0800 Subject: [PATCH 01/10] Refactored SHPrimitiveGenerator and added Sphere generation support --- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 165 ++++++++++++++---- .../MiddleEnd/Meshes/SHPrimitiveGenerator.h | 65 +++++-- 2 files changed, 182 insertions(+), 48 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index be181beb..6c708e58 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -3,7 +3,7 @@ \author Tng Kah Wei, kahwei.tng, 390009620 \par email: kahwei.tng\@digipen.edu \date Aug 30, 2022 -\brief Contains definitions for all of the functions of the classes that deal +\brief Contains definitions for all of the functions of the classes that deal with storage and management of vertex and index buffers of meshes. Copyright (C) 2022 DigiPen Institute of Technology. @@ -18,50 +18,59 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Static Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + SHMeshData SHPrimitiveGenerator::cubeMesh; + SHMeshData SHPrimitiveGenerator::sphereMesh; + + /*-----------------------------------------------------------------------------------*/ + /* Primitive Generation Functions */ + /*-----------------------------------------------------------------------------------*/ SHMeshData SHPrimitiveGenerator::Cube() noexcept { SHMeshData mesh; - mesh.VertexPositions = + mesh.VertexPositions = { // front - SHVec3(-0.5f, -0.5f, 0.5f), - SHVec3( 0.5f, -0.5f, 0.5f), - SHVec3( 0.5f, 0.5f, 0.5f), - SHVec3(-0.5f, 0.5f, 0.5f), + SHVec3(-0.5f, -0.5f, 0.5f), + SHVec3(0.5f, -0.5f, 0.5f), + SHVec3(0.5f, 0.5f, 0.5f), + SHVec3(-0.5f, 0.5f, 0.5f), // back - SHVec3(-0.5f, -0.5f, -0.5f), - SHVec3(-0.5f, 0.5f, -0.5f), - SHVec3( 0.5f, 0.5f, -0.5f), - SHVec3( 0.5f, -0.5f, -0.5f), + SHVec3(-0.5f, -0.5f, -0.5f), + SHVec3(-0.5f, 0.5f, -0.5f), + SHVec3(0.5f, 0.5f, -0.5f), + SHVec3(0.5f, -0.5f, -0.5f), - // top + // top SHVec3(-0.5f, 0.5f, -0.5f), SHVec3(-0.5f, 0.5f, 0.5f), - SHVec3( 0.5f, 0.5f, 0.5f), - SHVec3( 0.5f, 0.5f, -0.5f), - + SHVec3(0.5f, 0.5f, 0.5f), + SHVec3(0.5f, 0.5f, -0.5f), + // bottom - SHVec3(-0.5f, -0.5f, -0.5f), - SHVec3( 0.5f, -0.5f, -0.5f), - SHVec3( 0.5f, -0.5f, 0.5f), - SHVec3(-0.5f, -0.5f, 0.5f), - + SHVec3(-0.5f, -0.5f, -0.5f), + SHVec3(0.5f, -0.5f, -0.5f), + SHVec3(0.5f, -0.5f, 0.5f), + SHVec3(-0.5f, -0.5f, 0.5f), + // right - SHVec3(0.5f, -0.5f, -0.5f), - SHVec3(0.5f, 0.5f, -0.5f), - SHVec3(0.5f, 0.5f, 0.5f), - SHVec3(0.5f, -0.5f, 0.5f), - + SHVec3(0.5f, -0.5f, -0.5f), + SHVec3(0.5f, 0.5f, -0.5f), + SHVec3(0.5f, 0.5f, 0.5f), + SHVec3(0.5f, -0.5f, 0.5f), + // left - SHVec3(-0.5f, -0.5f, -0.5f), - SHVec3(-0.5f, -0.5f, 0.5f), - SHVec3(-0.5f, 0.5f, 0.5f), - SHVec3(-0.5f, 0.5f, -0.5f) + SHVec3(-0.5f, -0.5f, -0.5f), + SHVec3(-0.5f, -0.5f, 0.5f), + SHVec3(-0.5f, 0.5f, 0.5f), + SHVec3(-0.5f, 0.5f, -0.5f) }; - mesh.VertexTexCoords = + mesh.VertexTexCoords = { SHVec2(0.0f, 1.0f), SHVec2(1.0f, 1.0f), @@ -99,7 +108,7 @@ namespace SHADE SHVec2(0.0f, 0.0f) }; - mesh.VertexTangents = + mesh.VertexTangents = { // front SHVec3(1.0f, 0.0f, 0.0f), @@ -118,7 +127,7 @@ namespace SHADE SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 0.0f, 0.0f), - + // bottom SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 0.0f, 0.0f), @@ -193,10 +202,93 @@ namespace SHADE Handle SHPrimitiveGenerator::Cube(SHMeshLibrary& meshLibrary) noexcept { - static SHMeshData meshData = Cube(); + if (cubeMesh.VertexPositions.empty()) + cubeMesh = Cube(); + return addMeshDataTo(cubeMesh, meshLibrary); + } + + Handle SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept + { + if (cubeMesh.VertexPositions.empty()) + cubeMesh = Cube(); + return addMeshDataTo(cubeMesh, gfxSystem); + } + + SHADE::SHMeshData SHPrimitiveGenerator::Sphere() noexcept + { + SHMeshData meshData; + + const int LAT_SLICES = 8; + const int LONG_SLICES = 8; + float radius = 1; + for (int latNum = 0; latNum <= LAT_SLICES; ++latNum) + { + float theta = static_cast(latNum * std::numbers::pi / LAT_SLICES); + float sinTheta = sin(theta); + float cosTheta = cos(theta); + + for (int longNum = 0; longNum <= LONG_SLICES; ++longNum) + { + float phi = static_cast(longNum * 2 * std::numbers::pi / LONG_SLICES); + float sinPhi = sin(phi); + float cosPhi = cos(phi); + + const SHVec3 NORMAL = SHVec3(cosPhi * sinTheta, cosTheta, sinPhi * sinTheta); + meshData.VertexNormals.emplace_back(NORMAL); + meshData.VertexTangents.emplace_back(/* TODO */); + meshData.VertexTexCoords.emplace_back + ( + 1.0f - (longNum / static_cast(LONG_SLICES)), + 1.0f - (latNum / static_cast(LAT_SLICES)) + ); + meshData.VertexPositions.emplace_back(radius * NORMAL.x, radius * NORMAL.y, radius * NORMAL.z); + } + } + + for (int latNumber{}; latNumber < LAT_SLICES; latNumber++) + { + for (int longNumber{}; longNumber < LONG_SLICES; longNumber++) + { + const auto FIRST = (latNumber * (LONG_SLICES + 1)) + longNumber; + const auto SECOND = (FIRST + LONG_SLICES + 1); + + meshData.Indices.emplace_back(FIRST); + meshData.Indices.emplace_back(SECOND); + meshData.Indices.emplace_back(FIRST + 1); + + meshData.Indices.emplace_back(SECOND); + meshData.Indices.emplace_back(SECOND + 1); + meshData.Indices.emplace_back(FIRST + 1); + } + } + + return meshData; + } + + SHADE::Handle SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept + { + if (sphereMesh.VertexPositions.empty()) + sphereMesh = Sphere(); + + return addMeshDataTo(sphereMesh, meshLibrary); + } + + SHADE::Handle SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept + { + if (sphereMesh.VertexPositions.empty()) + sphereMesh = Sphere(); + + return addMeshDataTo(sphereMesh, gfxSystem); + } + + /*-----------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------------*/ + SHADE::Handle SHPrimitiveGenerator::addMeshDataTo(const SHMeshData& meshData, SHMeshLibrary& meshLibrary) noexcept + { return meshLibrary.AddMesh ( - static_cast(meshData.VertexPositions.size()), + static_cast(meshData.VertexPositions.size()), meshData.VertexPositions.data(), meshData.VertexTexCoords.data(), meshData.VertexTangents.data(), @@ -206,17 +298,16 @@ namespace SHADE ); } - Handle SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept + SHADE::Handle SHPrimitiveGenerator::addMeshDataTo(const SHMeshData& meshData, SHGraphicsSystem& gfxSystem) noexcept { - static SHMeshData meshData = Cube(); return gfxSystem.AddMesh ( - static_cast(meshData.VertexPositions.size()), + static_cast(meshData.VertexPositions.size()), meshData.VertexPositions.data(), meshData.VertexTexCoords.data(), meshData.VertexTangents.data(), meshData.VertexNormals.data(), - static_cast(meshData.Indices.size()), + static_cast(meshData.Indices.size()), meshData.Indices.data() ); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index dd059a29..ef4e26a6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -4,9 +4,9 @@ \par email: kahwei.tng\@digipen.edu \date Sep 18, 2022 \brief Contains the static class definition of SHPrimitiveGenerator. - + Copyright (C) 2022 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent +Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once @@ -29,12 +29,12 @@ namespace SHADE /*************************************************************************************/ /*! \brief - Static class that contains functions for generating 3D primitives. + Static class that contains functions for generating 3D primitives. */ /*************************************************************************************/ class SH_API SHPrimitiveGenerator { - public: + public: /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ @@ -42,7 +42,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Primitive Generation Functions */ - /*---------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ /***********************************************************************************/ /*! \brief @@ -52,20 +52,20 @@ namespace SHADE SHMeshData object containing vertex data for the cube. */ /***********************************************************************************/ - [[nodiscard]] static SHMeshData Cube() noexcept; + [[nodiscard]] static SHMeshData Cube() noexcept; /***********************************************************************************/ /*! \brief Produces a cube and constructs a SHMesh using the SHMeshLibrary provided. \param meshLibrary - Reference to the SHMeshLibrary to procude and store a cube mesh in. + Reference to the SHMeshLibrary to produce and store a cube mesh in. \return SHMesh object that points to the generated cube mesh in the SHMeshLibrary. */ /***********************************************************************************/ - [[nodiscard]] static Handle Cube(SHMeshLibrary& meshLibrary) noexcept; + [[nodiscard]] static Handle Cube(SHMeshLibrary& meshLibrary) noexcept; /***********************************************************************************/ /*! \brief @@ -78,12 +78,55 @@ namespace SHADE SHMesh object that points to the generated cube mesh in the SHGraphicsSystem. */ /***********************************************************************************/ - [[nodiscard]] static Handle Cube(SHGraphicsSystem& gfxSystem) noexcept; + [[nodiscard]] static Handle Cube(SHGraphicsSystem& gfxSystem) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a sphere and stores the data in a SHMeshData object. - private: + \return + SHMeshData object containing vertex data for the sphere. + */ + /***********************************************************************************/ + [[nodiscard]] static SHMeshData Sphere() noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a sphere and constructs a SHMesh using the SHMeshLibrary provided. + + \param meshLibrary + Reference to the SHMeshLibrary to produce and store a sphere mesh in. + + \return + SHMesh object that points to the generated sphere mesh in the SHMeshLibrary. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle Sphere(SHMeshLibrary& meshLibrary) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a sphere and constructs a SHMesh using the SHGraphicsSystem provided. + + \param gfxSystem + Reference to the SHGraphicsSystem to produce and store a sphere mesh in. + + \return + SHMesh object that points to the generated sphere mesh in the SHGraphicsSystem. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle Sphere(SHGraphicsSystem& gfxSystem) noexcept; + + private: /*---------------------------------------------------------------------------------*/ /* Helper Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static SHMeshData genCubeData() noexcept; + static Handle addMeshDataTo(const SHMeshData& meshData, SHMeshLibrary& meshLibrary) noexcept; + static Handle addMeshDataTo(const SHMeshData& meshData, SHGraphicsSystem& gfxSystem) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + static SHMeshData cubeMesh; + static SHMeshData sphereMesh; }; } \ No newline at end of file From 550b8d85f0eee7f157da51803b35e9910d0f4b87 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 26 Oct 2022 16:05:50 +0800 Subject: [PATCH 02/10] Added SHDebugDrawSystem class --- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 155 ++++++++++++++++++ .../MiddleEnd/Interface/SHDebugDrawSystem.h | 93 +++++++++++ .../MiddleEnd/Interface/SHDebugDrawSystem.hpp | 41 +++++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 7 + .../MiddleEnd/Interface/SHGraphicsSystem.h | 10 +- TempShaderFolder/DebugDrawFs.glsl | 17 ++ TempShaderFolder/DebugDrawFs.spv | Bin 0 -> 664 bytes TempShaderFolder/DebugDrawVs.glsl | 24 +++ TempShaderFolder/DebugDrawVs.spv | Bin 0 -> 1488 bytes 9 files changed, 340 insertions(+), 7 deletions(-) create mode 100644 SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp create mode 100644 SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h create mode 100644 SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp create mode 100644 TempShaderFolder/DebugDrawFs.glsl create mode 100644 TempShaderFolder/DebugDrawFs.spv create mode 100644 TempShaderFolder/DebugDrawVs.glsl create mode 100644 TempShaderFolder/DebugDrawVs.spv 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 0000000000000000000000000000000000000000..dd21ce44da7b58c2713eba6b923289359d901fb1 GIT binary patch literal 664 zcmY*W+e*Vg5FMM|Z0$`^-%79eFgs$wF0;Pb3wgupKqJzfrLc-N;R}ieKEBj#Tjcs;5c(UxSeG7s_+} zMd~iJ8lDb9clw4pRn{3lJt{x&Q{T*kXsO;)*E4gYBgAWr+mngyjMX#@Rh)cyT6xCT zYGxvIGAqqqS)E*G{Hd4DHU%XpHs4p0Quf9mO>P4(i0~{h7P|eeGUm2EuXOY$qK0$1 z!<1}P{WRF4Ef6(vj@2@cHW6G zYU}X$ij1R{nElD^12^D%IX=m86PSznEqH2K?;Nk^U3m{K_WJ@;f*)b0whd!_*0_R_ Sc^w$}Yh>`A#NViT0s8^rdQj8= literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..73e83f692e5dbd2a53de8fcea413846f1461c009 GIT binary patch literal 1488 zcmZ9LZ)+1l5XRS}m)6#{*4jU6jY*^RPoeliMGzH9<@zF$mV$4O<8sKt^%9axP+$2K z{7QZ*-v~ax+uIPjVQyxgd3JVYc2lo)mW{b#R?WIOHg0X1nh;~wOrz9c@3MC?DF!F6 zUcZE~Vd^Cj&ZfCp+DrOg#Wu%Tm)w^eNuEjS`qiZWXv30D3VTu5?}xq5Q8F4ulj28~ zMw!jiXkf=_QrKAvZ)JgJ^CX)N772kEi)RL^bxmh{%cApi{52gH>36w89gaQzx^>#i&`aZe~ZPc2dN7lFnmHfY>z4=CMG1(mN~AZ%euxU!~*t zG%jLS3m9wU3z6NZxYRNmeTfTc_HRok3;V&CYwX%nFYxN-u`m|BdV8h`bcfk+EF2DZ z(VrHwgYr(w92MbA7_(z}?J;TsLwk(5fH4D)VS{lep(5Dui2I&9@>yqMfsr#PeaFFv zbNd>m2U=p}WgmG%pBee@$fqB0^5Zi*FrV+PZ0d%;Q^m+j-}}<^K|RFpNy7yvA3n1N zBOlxYi7~%E!atP7UTSK};*DYxdm>E?j9mCf6-NB2Z1Tb12^!Hv&*OSBX>EVHd`Y*JH+8_W< u@)B!H_9QI{^)Z)@guCWGKd1wIc+|$8=Mwt${1>vBkLTmV`KQX>O8x`b2XP(% literal 0 HcmV?d00001 From 57027da80b966344746e7835219975a4e8128570 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 26 Oct 2022 16:27:58 +0800 Subject: [PATCH 03/10] 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 From 2108d9e1f6a2e1758b6c0b36b73ede9709731d6d Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 27 Oct 2022 10:12:30 +0800 Subject: [PATCH 04/10] Added triple buffering to SHDebugDrawSystem --- .../src/Application/SBApplication.cpp | 4 ++ SHADE_Application/src/Scenes/SBTestScene.cpp | 1 + .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 58 ++++++++++++++---- .../MiddleEnd/Interface/SHDebugDrawSystem.h | 29 ++++++--- .../MiddleEnd/Interface/SHDebugDrawSystem.hpp | 7 +++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 32 +++++++++- .../MiddleEnd/Interface/SHGraphicsSystem.h | 2 +- .../src/Graphics/Pipeline/SHVkPipeline.h | 3 +- TempShaderFolder/DebugDrawVs.glsl | 4 +- TempShaderFolder/DebugDrawVs.spv | Bin 1488 -> 1504 bytes 10 files changed, 114 insertions(+), 26 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 9b230296..29189d63 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -149,6 +149,10 @@ namespace Sandbox { SHFrameRateController::UpdateFRC(); SHInputManager::UpdateInput(SHFrameRateController::GetRawDeltaTime()); + + auto debugDraw = SHSystemManager::GetSystem(); + //debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 0.0f, 0.0f), SHVec3(5.0f, 0.0f, 0.0f)); + debugDraw->DrawCube(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, SHVec3(1.0f, 1.0f, 1.0f)); SHSceneManager::UpdateSceneManager(); #ifdef SHEDITOR if(editor->editorState == SHEditor::State::PLAY) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index a06e68c2..4172dffd 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -17,6 +17,7 @@ #include "Assets/SHAssetManager.h" #include "Camera/SHCameraComponent.h" #include "Resource/SHResourceManager.h" +#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" using namespace SHADE; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index 7e82fb21..444e9b57 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -11,6 +11,9 @@ of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #include "SHpch.h" #include "SHDebugDrawSystem.h" +// STL Includes +#include +// Project Includes #include "../Meshes/SHMeshData.h" #include "../Meshes/SHPrimitiveGenerator.h" #include "ECS_Base/Managers/SHSystemManager.h" @@ -26,25 +29,30 @@ namespace SHADE /* DrawRoutine */ /*---------------------------------------------------------------------------------*/ SHDebugDrawSystem::ProcessPointsRoutine::ProcessPointsRoutine() - : SHSystemRoutine("Debug Draw") - {} + : SHSystemRoutine("Debug Draw", true) + { + SystemFamily::GetID(); + } void SHDebugDrawSystem::ProcessPointsRoutine::Execute(double dt) noexcept { auto gfxSys = SHSystemManager::GetSystem(); - if (gfxSys) + if (!gfxSys) { SHLOG_WARNING("[DebugDraw] Attempted to do debug draw without a graphics system."); return; } + // Get current frame index + const uint32_t FRAME_IDX = gfxSys->GetCurrentFrameIndex(); + // Create the buffer if it doesn't exist or just update it SHDebugDrawSystem* system = static_cast(GetSystem()); - system->numPoints = system->points.size(); + system->numPoints[FRAME_IDX] = system->points.size(); const uint32_t DATA_SIZE = sizeof(PointVertex) * system->points.size(); if (DATA_SIZE > 0) { - SHVkUtil::EnsureBufferAndCopyHostVisibleData(gfxSys->GetDevice(), system->vertexBuffer, system->points.data(), DATA_SIZE, vk::BufferUsageFlagBits::eVertexBuffer); + system->vertexBuffers[FRAME_IDX]->WriteToMemory(system->points.data(), DATA_SIZE, 0, 0); } // Reset for next frame @@ -61,23 +69,45 @@ namespace SHADE 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) + subPass->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer) { + // Get Current frame index + const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); + // Don't draw if no points - if (numPoints <= 0) + if (numPoints[FRAME_IDX] <= 0) return; cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); - cmdBuffer->BindVertexBuffer(0, vertexBuffer, 0); - cmdBuffer->DrawArrays(static_cast(points.size()), 1, 0, 0); + cmdBuffer->BindVertexBuffer(0, vertexBuffers[FRAME_IDX], 0); + cmdBuffer->DrawArrays(numPoints[FRAME_IDX], 1, 0, 0); }); + + // Reset trackers + std::fill_n(numPoints.begin(), numPoints.size(), 0); + + // Allocate buffers + static constexpr uint32_t BUFFER_SIZE = MAX_POINTS * sizeof(PointVertex); + for (Handle& bufHandle : vertexBuffers) + { + bufHandle = GFX_SYSTEM->GetDevice()->CreateBuffer + ( + BUFFER_SIZE, + nullptr, + 0, + vk::BufferUsageFlagBits::eVertexBuffer, + VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, + VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT + ); + } } void SHDebugDrawSystem::Exit() { - if (vertexBuffer) + for (auto vertexBuffer : vertexBuffers) { - vertexBuffer.Free(); + if (vertexBuffer) + vertexBuffer.Free(); } } @@ -86,6 +116,12 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void SHDebugDrawSystem::DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) { + if (points.size() > MAX_POINTS) + { + SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); + return; + } + points.emplace_back(PointVertex{ startPt, color }); points.emplace_back(PointVertex{ endPt, color }); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index cc3e728a..2c6a6600 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -22,6 +22,7 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/System/SHSystemRoutine.h" #include "Resource/SHHandle.h" #include "Graphics/Buffers/SHVkBuffer.h" +#include "SHGraphicsConstants.h" namespace SHADE { @@ -34,7 +35,7 @@ namespace SHADE Manages the Debug Draw system. */ /***********************************************************************************/ - class SH_API SHDebugDrawSystem : public SHSystem + class SH_API SHDebugDrawSystem final : public SHSystem { public: /*-------------------------------------------------------------------------------*/ @@ -66,22 +67,30 @@ namespace SHADE void DrawSphere(const SHVec4& color, const SHVec3& pos, double radius); private: - /*-------------------------------------------------------------------------------*/ - /* Type Definitions */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ struct SH_API PointVertex { - SHVec3 Position; + SHVec4 Position; SHVec4 Color; }; - /*-------------------------------------------------------------------------------*/ - /* Data Members */ - /*-------------------------------------------------------------------------------*/ + using TripleBuffer = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; + using TripleUInt = std::array; + + /*---------------------------------------------------------------------------------*/ + /* Constants */ + /*---------------------------------------------------------------------------------*/ + static constexpr uint32_t MAX_POINTS = 100'000; + + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ // CPU Buffers std::vector points; // GPU Buffers - Handle vertexBuffer; - uint32_t numPoints = 0; + TripleBuffer vertexBuffers; + TripleUInt numPoints; // Cached Points for polygon drawing std::vector spherePoints; }; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp index 90c645e3..a913ecfd 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -24,6 +24,13 @@ namespace SHADE // Ensure dereferenced type is SHVec3 static_assert(std::is_same_v>, "Parameters to DrawPoly must be SHVec3."); + // Check if points exceeded max + if (points.size() > MAX_POINTS) + { + SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); + return; + } + const size_t POINTS_COUNT = pointListEnd - pointListBegin; // Invalid polygon if (POINTS_COUNT < 2) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 6bf52736..e6c1cd27 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -37,6 +37,7 @@ of DigiPen Institute of Technology is prohibited. #include "Assets/Asset Types/SHTextureAsset.h" #include "Graphics/MiddleEnd/Interface/SHMousePickSystem.h" #include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" +#include "Graphics/SHVkUtil.h" namespace SHADE { @@ -239,12 +240,41 @@ namespace SHADE .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() } ); - debugDrawPipeline = resourceManager.Create(device, debugDrawPipelineLayout); + debugDrawPipeline = resourceManager.Create(device, debugDrawPipelineLayout, nullptr, debugDrawNode->GetRenderpass(), debugDrawSubpass); debugDrawPipeline->GetPipelineState().SetRasterizationState(SHRasterizationState { .polygonMode = vk::PolygonMode::eLine, .cull_mode = vk::CullModeFlagBits::eNone }); + + SHVertexInputState debugDrawVertexInputState; + debugDrawVertexInputState.AddBinding(false, true, { SHVertexAttribute(SHAttribFormat::FLOAT_4D), SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); + debugDrawPipeline->GetPipelineState().SetVertexInputState(debugDrawVertexInputState); + SHColorBlendState colorBlendState{}; + colorBlendState.logic_op_enable = VK_FALSE; + colorBlendState.logic_op = vk::LogicOp::eCopy; + + auto const& subpassColorReferences = debugDrawSubpass->GetColorAttachmentReferences(); + colorBlendState.attachments.reserve(subpassColorReferences.size()); + + for (auto& att : subpassColorReferences) + { + colorBlendState.attachments.push_back(vk::PipelineColorBlendAttachmentState + { + .blendEnable = SHVkUtil::IsBlendCompatible(debugDrawSubpass->GetFormatFromAttachmentReference(att.attachment)), + .srcColorBlendFactor = vk::BlendFactor::eSrcAlpha, + .dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha, + .colorBlendOp = vk::BlendOp::eAdd, + .srcAlphaBlendFactor = vk::BlendFactor::eOne, + .dstAlphaBlendFactor = vk::BlendFactor::eZero, + .alphaBlendOp = vk::BlendOp::eAdd, + .colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA, + } + ); + } + + debugDrawPipeline->GetPipelineState().SetColorBlenState(colorBlendState); + debugDrawPipeline->ConstructPipeline(); } void SHGraphicsSystem::InitMiddleEnd(void) noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 0ede74c6..58f7cb6e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -288,7 +288,7 @@ namespace SHADE Handle GetMousePickSystem(void) const noexcept {return mousePickSystem;}; Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;}; Handle GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; } - + uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } private: diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipeline.h b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipeline.h index 7378cc48..7e313ed6 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipeline.h +++ b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipeline.h @@ -47,12 +47,13 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ + //! For constructing a graphics pipeline SHVkPipeline (Handle const& inLogicalDeviceHdl, Handle const& inPipelineLayout, SHVkPipelineState const* const state, Handle const& renderpassHdl, Handle subpass) noexcept; - + //! For constructing a compute pipeline SHVkPipeline(Handle const& inLogicalDeviceHdl, Handle const& inPipelineLayout) noexcept; diff --git a/TempShaderFolder/DebugDrawVs.glsl b/TempShaderFolder/DebugDrawVs.glsl index ae11658d..cb0886d1 100644 --- a/TempShaderFolder/DebugDrawVs.glsl +++ b/TempShaderFolder/DebugDrawVs.glsl @@ -1,7 +1,7 @@ #version 450 #extension GL_KHR_vulkan_glsl : enable -layout(location = 0) in vec3 aVertexPos; +layout(location = 0) in vec4 aVertexPos; layout(location = 1) in vec4 aVertColor; @@ -19,6 +19,6 @@ layout(set = 2, binding = 0) uniform CameraData void main() { - gl_Position = cameraData.vpMat * vec4 (aVertexPos, 1.0f); + gl_Position = cameraData.vpMat * vec4 (aVertexPos.xyz, 1.0f); Out.vertColor = aVertColor; } \ No newline at end of file diff --git a/TempShaderFolder/DebugDrawVs.spv b/TempShaderFolder/DebugDrawVs.spv index 73e83f692e5dbd2a53de8fcea413846f1461c009..46a663cfdfb38bf67aa8e976cfc3262fe113b3e9 100644 GIT binary patch delta 446 zcmXw!J5Iw;5Jl&iBp^i*LKIH;1oH{zQ_>(IKva|g(a^96RuGl|@=7c=0z06kprS_N z9Ls*z(VMw*$8Y9i_%(ce>`x-1te+lInpAaKAI-=;1HM`HnetRt-|2Tv-?XVj+(s|T z_n+)6EegUha)M>JXmjmqIr#?n0G8mC+;|%zUasxmC$eV$l*kc`tsQb(Q}f^EWvh{A zF|O*~e4%|4e0Cly&68WLrtWc^4DO@fjNmqUJe{^ftfsfqidM7tj=_9u7wp$xL_9b9 syzyg;dys>s8#MPjfDx?16&soByJRPOj^PM8{|Rvko!?x~AETYaKNkcX=Kufz delta 430 zcmX|+O-=$)5QOU?;DQh$E{1J7xkTc|jc36lXyybSfp8JHfm;`@T$}jS z_;4nb?y9ct*Zoo1RbHP;ElKI-`7wl6wh5=zA>3b?f0*q<64LB5e4p6Y%PZ2oib}U% zg{!bmNE^9bIwB6>O=1AnZz6ZQwHdrj>;UOyVgEUqdyhen%quu|?ULx4HP13nqei{1 z5yXStV8bYTFD}=9x()p7N~QeCZqx|>nlG}?6Ar$9)C$B0=KifBx!-=*z5W|>T{{B_ p7=Sx8&ROXKQUx_|%LV2(&RNk-6EFs`K~r+yb?}?p^TTS_;1^fg9BTjo From 605ff9710dd01586fc49801e8b23e86c6f76a1af Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sun, 30 Oct 2022 14:37:30 +0800 Subject: [PATCH 05/10] Added Matrix * Vec4 --- SHADE_Engine/src/Math/SHMatrix.cpp | 2 +- SHADE_Engine/src/Math/Vector/SHVec3.cpp | 4 +--- SHADE_Engine/src/Math/Vector/SHVec4.cpp | 12 +++++++++--- SHADE_Engine/src/Math/Vector/SHVec4.h | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/SHADE_Engine/src/Math/SHMatrix.cpp b/SHADE_Engine/src/Math/SHMatrix.cpp index ec3951e2..5f082ae5 100644 --- a/SHADE_Engine/src/Math/SHMatrix.cpp +++ b/SHADE_Engine/src/Math/SHMatrix.cpp @@ -191,7 +191,7 @@ namespace SHADE SHVec4 SHMatrix::operator*(const SHVec4& rhs) const noexcept { - return SHVec4::Transform3D(rhs, *this); + return SHVec4::Transform(rhs, *this); } SHMatrix SHMatrix::operator*(float rhs) const noexcept diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index c3e3acf2..cbd8ca32 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -445,9 +445,7 @@ namespace SHADE { SHVec3 result; - const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); - - XMStoreFloat3(&result, XMVector3TransformCoord(v, TF)); + XMStoreFloat3(&result, XMVector3TransformCoord(v, transformMtx)); return result; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.cpp b/SHADE_Engine/src/Math/Vector/SHVec4.cpp index 9857818a..88e7f5c9 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec4.cpp @@ -460,13 +460,19 @@ namespace SHADE return result; } + SHVec4 SHVec4::Transform(const SHVec4& v, const SHMatrix& transformMtx) noexcept + { + SHVec4 result; + + XMStoreFloat4(&result, XMVector4Transform(v, transformMtx)); + return result; + } + SHVec4 SHVec4::Transform3D(const SHVec4& v, const SHMatrix& transformMtx) noexcept { SHVec4 result; - const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); - - XMStoreFloat4(&result, XMVector3TransformCoord(v, TF)); + XMStoreFloat4(&result, XMVector3TransformCoord(v, transformMtx)); return result; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index ce341bed..db8ef860 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -134,6 +134,7 @@ namespace SHADE [[nodiscard]] static SHVec4 Project3D (const SHVec4& v, const SHVec4& u) noexcept; [[nodiscard]] static SHVec4 Reflect (const SHVec4& v, const SHVec4& normal) noexcept; [[nodiscard]] static SHVec4 Reflect3D (const SHVec4& v, const SHVec4& normal) noexcept; + [[nodiscard]] static SHVec4 Transform (const SHVec4& v, const SHMatrix& transformMtx) noexcept; [[nodiscard]] static SHVec4 Transform3D (const SHVec4& v, const SHMatrix& transformMtx) noexcept; }; From 03c0d100140b214458077aa50cc9ca10cde705d7 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sun, 30 Oct 2022 15:10:39 +0800 Subject: [PATCH 06/10] Fixed bug where objects without transform causes a crash on reparenting --- SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp index a2ab6880..c90e4431 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp @@ -263,6 +263,9 @@ namespace SHADE auto* node = EVENT_DATA->data->node; auto* tf = SHComponentManager::GetComponent_s(node->GetEntityID()); + if (tf == nullptr) + return EVENT_DATA->handle; + // Recompute local transform and store localToWorld Matrix SHMatrix localToWorld = SHMatrix::Identity; SHMatrix worldToLocal = SHMatrix::Identity; From 0096bc49757719b223dcc55b45852dde40d173db Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 1 Nov 2022 20:32:35 +0800 Subject: [PATCH 07/10] Debug Draw fixes --- .../src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h | 5 +++++ .../Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 2c6a6600..0f95e661 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -26,6 +26,11 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*---------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*---------------------------------------------------------------------------------*/ + class SHVkBuffer; + /*---------------------------------------------------------------------------------*/ /* Type Definitions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index e6c1cd27..0353c2ec 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -131,6 +131,9 @@ namespace SHADE shaderSourceLibrary.LoadShader(4, "DebugDrawVs.glsl", SH_SHADER_TYPE::VERTEX, true); shaderSourceLibrary.LoadShader(5, "DebugDrawFs.glsl", SH_SHADER_TYPE::FRAGMENT, true); + shaderSourceLibrary.LoadShader(3, "DebugDrawVs.glsl", SH_SHADER_TYPE::VERTEX, true); + shaderSourceLibrary.LoadShader(4, "DebugDrawFs.glsl", SH_SHADER_TYPE::FRAGMENT, true); + shaderModuleLibrary.ImportFromSourceLibrary(device, shaderSourceLibrary); auto cubeVS = shaderModuleLibrary.GetShaderModule("TestCubeVs.glsl"); auto cubeFS = shaderModuleLibrary.GetShaderModule("TestCubeFs.glsl"); @@ -214,6 +217,11 @@ namespace SHADE auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); dummySubpass->AddInput("Scene"); + // Set up Debug Draw Pass + auto debugDrawNode = worldRenderGraph->AddNode("Debug Draw", { "Scene" }, {"G-Buffer"}); + auto debugDrawSubpass = debugDrawNode->AddSubpass("Debug Draw"); + debugDrawSubpass->AddColorOutput("Scene"); + // Generate world render graph worldRenderGraph->Generate(); @@ -223,6 +231,7 @@ namespace SHADE worldRenderer->SetCameraDirector(cameraSystem->CreateDirector()); + // Create default material auto cubeVS = shaderModuleLibrary.GetShaderModule("TestCubeVs.glsl"); auto cubeFS = shaderModuleLibrary.GetShaderModule("TestCubeFs.glsl"); From dc8ed48be329690d2bb126cc43d9a6ad94095095 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 1 Nov 2022 21:49:57 +0800 Subject: [PATCH 08/10] Fixed debug draw system cube rendering --- .../src/Application/SBApplication.cpp | 5 +-- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 31 +++++++++---------- .../MiddleEnd/Interface/SHDebugDrawSystem.hpp | 6 ++-- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 54dcd094..4f03efc0 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -139,9 +139,10 @@ namespace Sandbox SHInputManager::UpdateInput(SHFrameRateController::GetRawDeltaTime()); auto debugDraw = SHSystemManager::GetSystem(); - debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 0.0f, 0.0f), SHVec3(5.0f, 0.0f, 0.0f)); - debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 1.0f, 0.0f), SHVec3(5.0f, 1.0f, 0.0f)); + /*debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 0.0f, 0.0f), SHVec3(5.0f, 0.0f, 0.0f)); + debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 1.0f, 0.0f), SHVec3(5.0f, 1.0f, 0.0f));*/ //debugDraw->DrawCube(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, SHVec3(1.0f, 1.0f, 1.0f)); + debugDraw->DrawSphere(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, 3.0f); SHSceneManager::UpdateSceneManager(); #ifdef SHEDITOR if(editor->editorState == SHEditor::State::PLAY) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index d54a8dad..08019665 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -145,14 +145,14 @@ namespace SHADE 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 }; + static const SHVec3 UNIT_BOT_LEFT_FRONT = SHVec3 { pos - EXTENTS }; + static const SHVec3 UNIT_BOT_RIGHT_FRONT = SHVec3 { pos + SHVec3 { EXTENTS.x, -EXTENTS.y, -EXTENTS.z } }; + static const SHVec3 UNIT_BOT_RIGHT_BACK = SHVec3 { pos + SHVec3 { EXTENTS.x, -EXTENTS.y, EXTENTS.z } }; + static const SHVec3 UNIT_BOT_LEFT_BACK = SHVec3 { pos + SHVec3 { -EXTENTS.x, -EXTENTS.y, EXTENTS.z } }; + static const SHVec3 UNIT_TOP_LEFT_BACK = SHVec3 { pos + SHVec3 { -EXTENTS.x, EXTENTS.y, EXTENTS.z } }; + static const SHVec3 UNIT_TOP_RIGHT_FRONT = SHVec3 { pos + SHVec3 { EXTENTS.x, 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_BACK = SHVec3 { pos + EXTENTS }; const SHVec3 BOT_LEFT_BACK = UNIT_BOT_LEFT_BACK * size; const SHVec3 BOT_RIGHT_BACK = UNIT_BOT_RIGHT_BACK * size; @@ -172,17 +172,16 @@ namespace SHADE 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 - + TOP_LEFT_FRONT , TOP_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 } ); } @@ -192,7 +191,7 @@ namespace SHADE if (spherePoints.empty()) { // Generate - const SHMeshData SPHERE = SHPrimitiveGenerator::Sphere(); + static const SHMeshData SPHERE = SHPrimitiveGenerator::Sphere(); for (const auto& idx : SPHERE.Indices) { spherePoints.emplace_back(SPHERE.VertexPositions[idx]); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp index a913ecfd..1fd90f38 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -39,10 +39,10 @@ namespace SHADE return; } - for (auto pointIter = pointListBegin + 1; pointIter != pointListEnd; ++pointIter) + const size_t POINTS_ROUNDED_COUNT = POINTS_COUNT / 2 * 2; + for (auto pointIter = pointListBegin; pointIter != (pointListBegin + POINTS_ROUNDED_COUNT); ++pointIter) { - points.emplace_back(PointVertex { *(pointIter - 1), color }); - points.emplace_back(PointVertex { *pointIter , color }); + points.emplace_back(PointVertex { *pointIter, color }); } } } \ No newline at end of file From b9ada6a5be49d3e696d20b990bd662a3fcba61c1 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 2 Nov 2022 00:47:19 +0800 Subject: [PATCH 09/10] Reworked SHColour to be compatible with SHVec4 --- SHADE_Application/src/Scenes/SBTestScene.cpp | 3 +- SHADE_Engine/src/Math/SHColour.cpp | 172 ++++++++++++++++++- SHADE_Engine/src/Math/SHColour.h | 50 ++++-- SHADE_Engine/src/Math/SHMath.h | 2 + SHADE_Engine/src/Math/Vector/SHVec4.cpp | 1 + SHADE_Engine/src/Math/Vector/SHVec4.h | 2 + 6 files changed, 206 insertions(+), 24 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 52f2dc7b..db58c21e 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -16,6 +16,7 @@ #include "Assets/SHAssetManager.h" #include "Camera/SHCameraComponent.h" +#include "Math/SHColour.h" #include "Resource/SHResourceManager.h" using namespace SHADE; @@ -159,7 +160,7 @@ namespace Sandbox SHComponentManager::RemoveComponent (0); auto ambientLight = SHEntityManager::CreateEntity(); - SHComponentManager::GetComponent(ambientLight)->SetColor(SHVec4(1.0f, 1.0f, 1.0f, 1.0f)); + SHComponentManager::GetComponent(ambientLight)->SetColor(SHColour::WHITE); SHComponentManager::GetComponent(ambientLight)->SetStrength(0.25f); SHComponentManager::GetComponent(ambientLight)->SetType(SH_LIGHT_TYPE::AMBIENT); } diff --git a/SHADE_Engine/src/Math/SHColour.cpp b/SHADE_Engine/src/Math/SHColour.cpp index 8aae2cb3..944c37cb 100644 --- a/SHADE_Engine/src/Math/SHColour.cpp +++ b/SHADE_Engine/src/Math/SHColour.cpp @@ -87,19 +87,19 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHColour::SHColour() noexcept - : SHVec4 { 0.0f, 0.0f, 0.0f, 1.0f } + : XMFLOAT4 { 0.0f, 0.0f, 0.0f, 1.0f } {} SHColour::SHColour(float r, float g, float b) noexcept - : SHVec4 { r, g, b, 1.0f } + : XMFLOAT4 { r, g, b, 1.0f } {} SHColour::SHColour(float r, float g, float b, float a) noexcept - : SHVec4 { r, g, b, a } + : XMFLOAT4 { r, g, b, a } {} SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b) noexcept - : SHVec4 + : XMFLOAT4 { static_cast(r) / 255.0f, static_cast(g) / 255.0f, @@ -109,7 +109,7 @@ namespace SHADE {} SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept - : SHVec4 + : XMFLOAT4 { static_cast(r) / 255.0f, static_cast(g) / 255.0f, @@ -118,12 +118,24 @@ namespace SHADE } {} - SHColour::SHColour(const DirectX::XMFLOAT3& colour) noexcept - : SHVec4 { colour.x, colour.y, colour.z, 1.0f } + SHColour::SHColour(const SHVec3& rgb) noexcept + : XMFLOAT4 { rgb.x, rgb.y, rgb.z, 1.0f } + {} + + SHColour::SHColour(const SHVec4& rgba) noexcept + : XMFLOAT4 { rgba.x, rgba.y, rgba.z, rgba.w } + {} + + SHColour::SHColour(const DirectX::XMFLOAT3& rgb) noexcept + : XMFLOAT4 { rgb.x, rgb.y, rgb.z, 1.0f } + {} + + SHColour::SHColour(const DirectX::XMFLOAT4& rgba) noexcept + : XMFLOAT4 { rgba.x, rgba.y, rgba.z, rgba.w } {} SHColour::SHColour(const DirectX::XMVECTORF32& colour) noexcept - : SHVec4 + : XMFLOAT4 { XMVectorGetX(colour), XMVectorGetY(colour), @@ -136,6 +148,86 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ + SHColour::operator XMVECTOR() const noexcept + { + return XMLoadFloat4(this); + } + + SHColour::operator SHVec4() const noexcept + { + return SHVec4{ *this }; + } + + SHColour& SHColour::operator+=(const SHColour& rhs) noexcept + { + return *this = *this + rhs; + } + + SHColour& SHColour::operator-=(const SHColour& rhs) noexcept + { + return *this = *this - rhs; + } + + SHColour& SHColour::operator*=(const SHColour& rhs) noexcept + { + return *this = *this * rhs; + } + + SHColour& SHColour::operator*=(float rhs) noexcept + { + return *this = *this * rhs; + } + + SHColour& SHColour::operator/=(const SHColour& rhs) noexcept + { + return *this = *this / rhs; + } + + SHColour SHColour::operator+(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorAdd(*this, rhs)); + return result; + } + + SHColour SHColour::operator-(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorSubtract(*this, rhs)); + return result; + } + + SHColour SHColour::operator-() const noexcept + { + return SHColour{ -x, -y, -z, -w }; + } + + SHColour SHColour::operator*(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorMultiply(*this, rhs)); + return result; + } + + SHColour SHColour::operator*(float rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorScale(*this, rhs)); + return result; + } + + SHColour SHColour::operator/(const SHColour& rhs) const noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorDivide(*this, rhs)); + return result; + } + bool SHColour::operator==(const SHColour& rhs) const noexcept { return XMColorEqual(*this, rhs); @@ -146,6 +238,70 @@ namespace SHADE return XMColorNotEqual(*this, rhs); } + float& SHColour::operator[](int index) + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float& SHColour::operator[](size_t index) + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float SHColour::operator[](int index) const + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + float SHColour::operator[](size_t index) const + { + if (index >= SIZE || index < 0) + throw std::invalid_argument("Index out of range!"); + + switch (index) + { + case 0: return x; + case 1: return y; + case 2: return z; + case 3: return w; + } + } + + SHColour operator* (float lhs, const SHColour& rhs) noexcept + { + SHColour result; + + XMStoreFloat4(&result, XMVectorScale(rhs, lhs)); + return result; + } + /*-----------------------------------------------------------------------------------*/ /* Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHColour.h b/SHADE_Engine/src/Math/SHColour.h index bd2bc9e7..a6adf7bb 100644 --- a/SHADE_Engine/src/Math/SHColour.h +++ b/SHADE_Engine/src/Math/SHColour.h @@ -19,12 +19,6 @@ namespace SHADE { - /*-----------------------------------------------------------------------------------*/ - /* Forward Declarations */ - /*-----------------------------------------------------------------------------------*/ - - class SHColour; - /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -40,7 +34,7 @@ namespace SHADE float v = 0.0f; }; - class SH_API SHColour : private SHVec4 + class SH_API SHColour : public DirectX::XMFLOAT4 { public: /*---------------------------------------------------------------------------------*/ @@ -53,9 +47,11 @@ namespace SHADE SHColour (uint8_t r, uint8_t g, uint8_t b) noexcept; SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept; - SHColour (const SHVec3& colour) noexcept; - SHColour (const DirectX::XMFLOAT3& colour) noexcept; - SHColour (const DirectX::XMVECTORF32& colour) noexcept; + SHColour (const SHVec3& rgb) noexcept; + SHColour (const SHVec4& rgba) noexcept; + SHColour (const DirectX::XMFLOAT3& rgb) noexcept; + SHColour (const DirectX::XMFLOAT4& rgba) noexcept; + SHColour (const DirectX::XMVECTORF32& rgba) noexcept; SHColour (const SHColour&) = default; SHColour (SHColour&&) = default; @@ -66,11 +62,32 @@ namespace SHADE /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHColour& operator= (const SHColour&) = default; - SHColour& operator= (SHColour&&) = default; + SHColour& operator= (const SHColour&) = default; + SHColour& operator= (SHColour&&) = default; - bool operator== (const SHColour& rhs) const noexcept; - bool operator!= (const SHColour& rhs) const noexcept; + operator DirectX::XMVECTOR () const noexcept; + operator SHVec4 () const noexcept; + + SHColour& operator+= (const SHColour& rhs) noexcept; + SHColour& operator-= (const SHColour& rhs) noexcept; + SHColour& operator*= (const SHColour& rhs) noexcept; + SHColour& operator*= (float rhs) noexcept; + SHColour& operator/= (const SHColour& rhs) noexcept; + + [[nodiscard]] SHColour operator+ (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator- (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator- () const noexcept; + [[nodiscard]] SHColour operator* (const SHColour& rhs) const noexcept; + [[nodiscard]] SHColour operator* (float rhs) const noexcept; + [[nodiscard]] SHColour operator/ (const SHColour& rhs) const noexcept; + + [[nodiscard]] bool operator== (const SHColour& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHColour& rhs) const noexcept; + + [[nodiscard]] float& operator[] (int index); + [[nodiscard]] float& operator[] (size_t index); + [[nodiscard]] float operator[] (int index) const; + [[nodiscard]] float operator[] (size_t index) const; /*---------------------------------------------------------------------------------*/ /* Properties */ @@ -105,6 +122,8 @@ namespace SHADE /* Static Data Members */ /*---------------------------------------------------------------------------------*/ + static constexpr size_t SIZE = 4U; + static const SHColour BEIGE ; static const SHColour BLACK ; static const SHColour BLUE ; @@ -159,8 +178,9 @@ namespace SHADE static const SHColour TURQUOISE ; static const SHColour VIOLET ; static const SHColour WHITE ; - static const SHColour YELLOW; + static const SHColour YELLOW ; }; + SHColour operator* (float lhs, const SHColour& rhs) noexcept; } // namespace SHADE diff --git a/SHADE_Engine/src/Math/SHMath.h b/SHADE_Engine/src/Math/SHMath.h index 5fcea9fc..3a24d6ef 100644 --- a/SHADE_Engine/src/Math/SHMath.h +++ b/SHADE_Engine/src/Math/SHMath.h @@ -9,4 +9,6 @@ #include "SHQuaternion.h" #include "SHMatrix.h" +#include "SHColour.h" + #include "Transform/SHTransform.h" \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.cpp b/SHADE_Engine/src/Math/Vector/SHVec4.cpp index 88e7f5c9..943d540e 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec4.cpp @@ -14,6 +14,7 @@ #include "SHVec4.h" // Project Headers #include "Math/SHMatrix.h" +#include "Math/SHColour.h" #include "Tools/SHLogger.h" using namespace DirectX; diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index db8ef860..3c509039 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -23,7 +23,9 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ + class SHMatrix; + class SHColour; /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ From 5f7b28e8a0c9a137bbe2de82186d56e64490a8f9 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 2 Nov 2022 01:11:28 +0800 Subject: [PATCH 10/10] Added SHDebugDraw convenience static class for debug drawing --- .../src/Application/SBApplication.cpp | 10 +- SHADE_Application/src/Scenes/SBTestScene.cpp | 3 + .../MiddleEnd/Interface/SHDebugDrawSystem.h | 98 ++++++++++++----- .../MiddleEnd/Interface/SHDebugDrawSystem.hpp | 20 ++-- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 4 +- SHADE_Engine/src/Tools/SHDebugDraw.cpp | 73 +++++++++++++ SHADE_Engine/src/Tools/SHDebugDraw.h | 101 ++++++++++++++++++ 7 files changed, 267 insertions(+), 42 deletions(-) create mode 100644 SHADE_Engine/src/Tools/SHDebugDraw.cpp create mode 100644 SHADE_Engine/src/Tools/SHDebugDraw.h diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 4f03efc0..b865f028 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -43,6 +43,7 @@ #include "Assets/SHAssetManager.h" #include "Tools/SHLogger.h" +#include "Tools/SHDebugDraw.h" using namespace SHADE; @@ -126,6 +127,9 @@ namespace Sandbox SHSceneManager::InitSceneManager("TestScene"); SHFrameRateController::UpdateFRC(); + + // Link up SHDebugDraw + SHDebugDraw::Init(SHSystemManager::GetSystem()); } void SBApplication::Update(void) @@ -137,12 +141,6 @@ namespace Sandbox { SHFrameRateController::UpdateFRC(); SHInputManager::UpdateInput(SHFrameRateController::GetRawDeltaTime()); - - auto debugDraw = SHSystemManager::GetSystem(); - /*debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 0.0f, 0.0f), SHVec3(5.0f, 0.0f, 0.0f)); - debugDraw->DrawLine(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), SHVec3(-5.0f, 1.0f, 0.0f), SHVec3(5.0f, 1.0f, 0.0f));*/ - //debugDraw->DrawCube(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, SHVec3(1.0f, 1.0f, 1.0f)); - debugDraw->DrawSphere(SHVec4(1.0f, 1.0f, 1.0f, 1.0f), {}, 3.0f); SHSceneManager::UpdateSceneManager(); #ifdef SHEDITOR if(editor->editorState == SHEditor::State::PLAY) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 5b53eef9..a5b3b546 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -19,6 +19,7 @@ #include "Math/SHColour.h" #include "Resource/SHResourceManager.h" #include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" +#include "Tools/SHDebugDraw.h" using namespace SHADE; @@ -188,6 +189,8 @@ namespace Sandbox SHADE::SHScriptEngine* scriptEngine = static_cast(SHADE::SHSystemManager::GetSystem()); scriptEngine->RemoveAllScripts(testObj); } + + SHDebugDraw::Cube(SHColour::CRIMSON, SHVec3(1.0f, 0.0f, 0.0f), SHVec3(1.0f, 1.0f, 1.0f)); } void SBTestScene::Render() diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 1bbf3ab8..4b83958d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -23,29 +23,27 @@ of DigiPen Institute of Technology is prohibited. #include "Resource/SHHandle.h" #include "Graphics/Buffers/SHVkBuffer.h" #include "SHGraphicsConstants.h" +#include "Math/SHColour.h" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Forward Declarations */ - /*---------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ class SHVkBuffer; - /*---------------------------------------------------------------------------------*/ - /* Type Definitions */ - /*---------------------------------------------------------------------------------*/ - /***********************************************************************************/ - /*! - \brief - Manages the Debug Draw system. - */ - /***********************************************************************************/ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + /// + /// Manages the Debug Draw system. + /// class SH_API SHDebugDrawSystem final : public SHSystem { public: - /*-------------------------------------------------------------------------------*/ - /* System Routines */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* System Routines */ + /*---------------------------------------------------------------------------------*/ class SH_API ProcessPointsRoutine final : public SHSystemRoutine { public: @@ -53,27 +51,79 @@ namespace SHADE virtual void Execute(double dt) noexcept override final; }; - /*-------------------------------------------------------------------------------*/ - /* SHSystem overrides */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* SHSystem overrides */ + /*---------------------------------------------------------------------------------*/ virtual void Init() override final; virtual void Exit() override final; - /*-------------------------------------------------------------------------------*/ - /* Configuration Functions */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* Configuration Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Configures the line width used to draw all lines in the Debug Draw system. + /// float LineWidth = 1.0f; - /*-------------------------------------------------------------------------------*/ - /* Draw Functions */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Renders a line between two points in world space. + /// + /// Colour of the line. + /// First point of the line. + /// Second point of the line. void DrawLine(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt); + /// + /// Renders a triangle indicated by three points in world space. + /// + /// Colour of the triangle. + /// First point of the triangle. + /// Second point of the triangle. + /// Third point of the triangle. void DrawTri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3); + /// + /// Renders a quadrilateral indicated by four points in world space. + /// + /// Colour of the quadrilateral. + /// First point of the triangle. + /// Second point of the quadrilateral. + /// Third point of the quadrilateral. + /// Third point of the quadrilateral. void DrawQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); + /// + /// Renders a polygon indicated by the specified set of points in world space. + /// + /// Colour of the polygon. + /// List of points for the polygon. void DrawPoly(const SHVec4& color, std::initializer_list pointList); + /// + /// Renders a polygon indicated by the specified set of points in world space. + /// + /// Iterator for a STL-like container. + /// Colour of the polygon. + /// + /// Iterator to the first point of the point container. + /// + /// + /// One past last iterator of the point container. + /// template void DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd); + /// + /// Renders a wireframe cube centered around the position specified in world space. + /// + /// Colour of the cube. + /// Position where the cube wil be centered at. + /// Size of the rendered cube. void DrawCube(const SHVec4& color, const SHVec3& pos, const SHVec3& size); + /// + /// Renders a wireframe sphere centered around the position specified in world space. + /// + /// Colour of the sphere. + /// Position where the sphere wil be centered at. + /// Size of the rendered sphere. void DrawSphere(const SHVec4& color, const SHVec3& pos, double radius); private: diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp index 1fd90f38..14fbb42f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -3,7 +3,7 @@ \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 +\brief Contains the definition of template functions the SHDebugDrawSystem class. Copyright (C) 2022 DigiPen Institute of Technology. @@ -15,11 +15,11 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Template Functions */ - /*---------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*-----------------------------------------------------------------------------------*/ template - void SHADE::SHDebugDrawSystem::DrawPoly(const SHVec4& color, IterType pointListBegin, IterType pointListEnd) + void 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."); @@ -27,22 +27,22 @@ namespace SHADE // Check if points exceeded max if (points.size() > MAX_POINTS) { - SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); - return; + SHLOG_WARNING("[DebugDraw] Exceeded maximum size of drawable debug elements."); + return; } const size_t POINTS_COUNT = pointListEnd - pointListBegin; // Invalid polygon if (POINTS_COUNT < 2) { - SHLOG_WARNING("[SHDebugDraw] Invalid polygon provided to DrawPoly()."); - return; + SHLOG_WARNING("[SHDebugDraw] Invalid polygon provided to DrawPoly()."); + return; } const size_t POINTS_ROUNDED_COUNT = POINTS_COUNT / 2 * 2; for (auto pointIter = pointListBegin; pointIter != (pointListBegin + POINTS_ROUNDED_COUNT); ++pointIter) { - points.emplace_back(PointVertex { *pointIter, color }); + points.emplace_back(PointVertex{ *pointIter, color }); } } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index 6c708e58..c9a3f6c5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -218,8 +218,8 @@ namespace SHADE { SHMeshData meshData; - const int LAT_SLICES = 8; - const int LONG_SLICES = 8; + const int LAT_SLICES = 12; + const int LONG_SLICES = 12; float radius = 1; for (int latNum = 0; latNum <= LAT_SLICES; ++latNum) { diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.cpp b/SHADE_Engine/src/Tools/SHDebugDraw.cpp new file mode 100644 index 00000000..a5b86c42 --- /dev/null +++ b/SHADE_Engine/src/Tools/SHDebugDraw.cpp @@ -0,0 +1,73 @@ +/************************************************************************************//*! +\file SHDebugDrawSystem.cpp +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 2, 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. +*//*************************************************************************************/ +// Precompiled Header +#include "SHpch.h" +// Primary Include +#include "SHDebugDraw.h" +// Project Includes +#include "Math/Vector/SHVec4.h" +#include "Math/SHColour.h" +#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" +#include "ECS_Base/Managers/SHSystemManager.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Static Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + SHDebugDrawSystem* SHDebugDraw::dbgDrawSys = nullptr; + + /*-----------------------------------------------------------------------------------*/ + /* Lifecycle Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDraw::Init(SHDebugDrawSystem* sys) + { + dbgDrawSys = sys; + if (dbgDrawSys == nullptr) + { + SHLOG_ERROR("[SHDebugDraw] Invalid SHDebugDrawSystem provided for initialization!"); + } + } + + /*-----------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHDebugDraw::Line(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt) + { + dbgDrawSys->DrawLine(color, startPt, endPt); + } + + void SHDebugDraw::Tri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3) + { + dbgDrawSys->DrawTri(color, pt1, pt2, pt3); + } + + void SHDebugDraw::Quad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4) + { + dbgDrawSys->DrawQuad(color, pt1, pt2, pt3, pt4); + } + + void SHDebugDraw::Poly(const SHVec4& color, std::initializer_list pointList) + { + dbgDrawSys->DrawPoly(color, pointList); + } + + void SHDebugDraw::Cube(const SHVec4& color, const SHVec3& pos, const SHVec3& size) + { + dbgDrawSys->DrawCube(color, pos, size); + } + + void SHDebugDraw::Sphere(const SHVec4& color, const SHVec3& pos, double radius) + { + dbgDrawSys->DrawSphere(color, pos, radius); + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.h b/SHADE_Engine/src/Tools/SHDebugDraw.h new file mode 100644 index 00000000..7ce44ec2 --- /dev/null +++ b/SHADE_Engine/src/Tools/SHDebugDraw.h @@ -0,0 +1,101 @@ +/************************************************************************************//*! +\file SHDebugDraw.h +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 2, 2022 +\brief Contains the definition of the SHDebugDraw static 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 + +// Project Includes +#include "SH_API.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + class SHDebugDrawSystem; + class SHVec4; + class SHVec3; + class SHColour; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + /// + /// Static helper class to make it easier to do debug drawing and enable support for + /// managed code to execute it withot dependency hell due to graphics system + /// dependencies. + /// + class SH_API SHDebugDraw final + { + public: + /*---------------------------------------------------------------------------------*/ + /* Lifecycle Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Sets up the link with the SHDebugDrawSystem. Must be called after the + /// SHDebugDrawSystem is spawned. + /// + static void Init(SHDebugDrawSystem* sys); + + /*---------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Renders a line between two points in world space. + /// + /// Colour of the line. + /// First point of the line. + /// Second point of the line. + static void Line(const SHVec4& color, const SHVec3& startPt, const SHVec3& endPt); + /// + /// Renders a triangle indicated by three points in world space. + /// + /// Colour of the triangle. + /// First point of the triangle. + /// Second point of the triangle. + /// Third point of the triangle. + static void Tri(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3); + /// + /// Renders a quadrilateral indicated by four points in world space. + /// + /// Colour of the quadrilateral. + /// First point of the triangle. + /// Second point of the quadrilateral. + /// Third point of the quadrilateral. + /// Third point of the quadrilateral. + static void Quad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); + /// + /// Renders a polygon indicated by the specified set of points in world space. + /// + /// Colour of the polygon. + /// List of points for the polygon. + static void Poly(const SHVec4& color, std::initializer_list pointList); + /// + /// Renders a wireframe cube centered around the position specified in world space. + /// + /// Colour of the cube. + /// Position where the cube wil be centered at. + /// Size of the rendered cube. + static void Cube(const SHVec4& color, const SHVec3& pos, const SHVec3& size); + /// + /// Renders a wireframe sphere centered around the position specified in world space. + /// + /// Colour of the sphere. + /// Position where the sphere wil be centered at. + /// Size of the rendered sphere. + static void Sphere(const SHVec4& color, const SHVec3& pos, double radius); + + private: + /*---------------------------------------------------------------------------------*/ + /* Static Data Members */ + /*---------------------------------------------------------------------------------*/ + static SHDebugDrawSystem* dbgDrawSys; + }; +} \ No newline at end of file