diff --git a/Assets/Shaders/DebugDraw_FS.glsl b/Assets/Shaders/DebugDraw_FS.glsl new file mode 100644 index 00000000..266f8ad4 --- /dev/null +++ b/Assets/Shaders/DebugDraw_FS.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/Assets/Shaders/DebugDraw_FS.shshaderb b/Assets/Shaders/DebugDraw_FS.shshaderb new file mode 100644 index 00000000..7b596885 Binary files /dev/null and b/Assets/Shaders/DebugDraw_FS.shshaderb differ diff --git a/Assets/Shaders/DebugDraw_FS.shshaderb.shmeta b/Assets/Shaders/DebugDraw_FS.shshaderb.shmeta new file mode 100644 index 00000000..58f62b44 --- /dev/null +++ b/Assets/Shaders/DebugDraw_FS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: DebugDraw_FS +ID: 36671027 +Type: 2 diff --git a/Assets/Shaders/DebugDraw_VS.glsl b/Assets/Shaders/DebugDraw_VS.glsl new file mode 100644 index 00000000..cb0886d1 --- /dev/null +++ b/Assets/Shaders/DebugDraw_VS.glsl @@ -0,0 +1,24 @@ +#version 450 +#extension GL_KHR_vulkan_glsl : enable + +layout(location = 0) in vec4 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.xyz, 1.0f); + Out.vertColor = aVertColor; +} \ No newline at end of file diff --git a/Assets/Shaders/DebugDraw_VS.shshaderb b/Assets/Shaders/DebugDraw_VS.shshaderb new file mode 100644 index 00000000..2d8cd0ed Binary files /dev/null and b/Assets/Shaders/DebugDraw_VS.shshaderb differ diff --git a/Assets/Shaders/DebugDraw_VS.shshaderb.shmeta b/Assets/Shaders/DebugDraw_VS.shshaderb.shmeta new file mode 100644 index 00000000..ab840f4c --- /dev/null +++ b/Assets/Shaders/DebugDraw_VS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: DebugDraw_VS +ID: 48002439 +Type: 2 diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index a4fef1fa..b865f028 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" @@ -42,6 +43,7 @@ #include "Assets/SHAssetManager.h" #include "Tools/SHLogger.h" +#include "Tools/SHDebugDraw.h" using namespace SHADE; @@ -69,6 +71,7 @@ namespace Sandbox SHGraphicsSystem* graphicsSystem = static_cast(SHSystemManager::GetSystem()); SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); #ifdef SHEDITOR SDL_Init(SDL_INIT_VIDEO); @@ -90,6 +93,7 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); @@ -123,6 +127,9 @@ namespace Sandbox SHSceneManager::InitSceneManager("TestScene"); SHFrameRateController::UpdateFRC(); + + // Link up SHDebugDraw + SHDebugDraw::Init(SHSystemManager::GetSystem()); } void SBApplication::Update(void) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 52f2dc7b..a5b3b546 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -16,7 +16,10 @@ #include "Assets/SHAssetManager.h" #include "Camera/SHCameraComponent.h" +#include "Math/SHColour.h" #include "Resource/SHResourceManager.h" +#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" +#include "Tools/SHDebugDraw.h" using namespace SHADE; @@ -159,7 +162,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); } @@ -186,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/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index a744c795..fb64bcc2 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -282,6 +282,11 @@ namespace SHADE } + void SHVkCommandBuffer::SetLineWidth(float lineWidth) noexcept + { + vkCommandBuffer.setLineWidth(lineWidth); + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index 4c978c34..bc0d4b04 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -115,6 +115,7 @@ namespace SHADE // Dynamic State void SetViewportScissor (float vpWidth, float vpHeight, uint32_t sWidth, uint32_t sHeight, float vpX = 0.0f, float vpY = 0.0f, int32_t sX = 0.0f, int32_t sY = 0.0f, float vpMinDepth = 0.0f, float vpMaxDepth = 1.0f) noexcept; + void SetLineWidth (float lineWidth) noexcept; // Binding Commands void BindPipeline (Handle const& pipelineHdl) noexcept; 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..08019665 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -0,0 +1,202 @@ +/************************************************************************************//*! +\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" +// STL Includes +#include +// Project Includes +#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::ProcessPointsRoutine::ProcessPointsRoutine() + : SHSystemRoutine("Debug Draw", true) + { + SystemFamily::GetID(); + } + + void SHDebugDrawSystem::ProcessPointsRoutine::Execute(double dt) noexcept + { + auto gfxSys = SHSystemManager::GetSystem(); + 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[FRAME_IDX] = system->points.size(); + const uint32_t DATA_SIZE = sizeof(PointVertex) * system->points.size(); + if (DATA_SIZE > 0) + { + system->vertexBuffers[FRAME_IDX]->WriteToMemory(system->points.data(), DATA_SIZE, 0, 0); + } + + // 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([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[FRAME_IDX] <= 0) + return; + + cmdBuffer->BindPipeline(GFX_SYSTEM->GetDebugDrawPipeline()); + cmdBuffer->SetLineWidth(LineWidth); + 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() + { + for (auto vertexBuffer : vertexBuffers) + { + if (vertexBuffer) + vertexBuffer.Free(); + } + } + + /*---------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*---------------------------------------------------------------------------------*/ + 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 }); + } + + 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_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; + 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, + // 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, + // 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 + } + ); + } + + void SHDebugDrawSystem::DrawSphere(const SHVec4& color, const SHVec3& pos, double radius) + { + if (spherePoints.empty()) + { + // Generate + static 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..4b83958d --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -0,0 +1,159 @@ +/************************************************************************************//*! +\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" +#include "Graphics/Buffers/SHVkBuffer.h" +#include "SHGraphicsConstants.h" +#include "Math/SHColour.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + class SHVkBuffer; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + /// + /// Manages the Debug Draw system. + /// + class SH_API SHDebugDrawSystem final : public SHSystem + { + public: + /*---------------------------------------------------------------------------------*/ + /* System Routines */ + /*---------------------------------------------------------------------------------*/ + class SH_API ProcessPointsRoutine final : public SHSystemRoutine + { + public: + ProcessPointsRoutine(); + virtual void Execute(double dt) noexcept override final; + }; + + /*---------------------------------------------------------------------------------*/ + /* SHSystem overrides */ + /*---------------------------------------------------------------------------------*/ + virtual void Init() override final; + virtual void Exit() override final; + + /*---------------------------------------------------------------------------------*/ + /* Configuration Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Configures the line width used to draw all lines in the Debug Draw system. + /// + float LineWidth = 1.0f; + + /*---------------------------------------------------------------------------------*/ + /* 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: + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + struct SH_API PointVertex + { + SHVec4 Position; + SHVec4 Color; + }; + 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 + TripleBuffer vertexBuffers; + TripleUInt numPoints; + // 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..14fbb42f --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.hpp @@ -0,0 +1,48 @@ +/************************************************************************************//*! +\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 +{ + /*-----------------------------------------------------------------------------------*/ + /* Draw Functions */ + /*-----------------------------------------------------------------------------------*/ + template + 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."); + + // 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) + { + 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 }); + } + } +} \ 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 007c8639..2bc2b7de 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -39,6 +39,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" #include "Assets/SHAssetManager.h" #include "Resource/SHResourceManager.h" +#include "Graphics/SHVkUtil.h" namespace SHADE { @@ -111,6 +112,8 @@ namespace SHADE // Load Built In Shaders static constexpr AssetID VS_DEFAULT = 39210065; defaultVertShader = SHResourceManager::LoadOrGet(VS_DEFAULT); static constexpr AssetID FS_DEFAULT = 46377769; defaultFragShader = SHResourceManager::LoadOrGet(FS_DEFAULT); + static constexpr AssetID VS_DEBUG = 48002439; debugVertShader = SHResourceManager::LoadOrGet(VS_DEBUG); + static constexpr AssetID FS_DEBUG = 36671027; debugFragShader = SHResourceManager::LoadOrGet(FS_DEBUG); static constexpr AssetID CS_COMPOSITE = 45072428; deferredCompositeShader = SHResourceManager::LoadOrGet(CS_COMPOSITE); } @@ -183,12 +186,16 @@ namespace SHADE // deferred composite gBufferNode->AddNodeCompute(deferredCompositeShader, { "Position", "Normals", "Albedo", "Light Layer Indices", "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 + // Dummy Node + 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(); @@ -198,7 +205,57 @@ namespace SHADE worldRenderer->SetCameraDirector(cameraSystem->CreateDirector()); + // Create default materials defaultMaterial = AddMaterial(defaultVertShader, defaultFragShader, gBufferSubpass); + + // Create debug draw pipeline + auto debugDrawPipelineLayout = resourceManager.Create + ( + device, SHPipelineLayoutParams + { + .shaderModules = { debugVertShader, debugFragShader }, + .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() + } + ); + debugDrawPipeline = resourceManager.Create(device, debugDrawPipelineLayout, nullptr, debugDrawNode->GetRenderpass(), debugDrawSubpass); + debugDrawPipeline->GetPipelineState().SetRasterizationState(SHRasterizationState + { + .polygonMode = vk::PolygonMode::eLine, + .cull_mode = vk::CullModeFlagBits::eNone + }); + debugDrawPipeline->GetPipelineState().SetInputAssemblyState(SHInputAssemblyState + { + .topology = vk::PrimitiveTopology::eLineList + }); + + 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 3f899446..b307dc92 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -288,9 +288,8 @@ namespace SHADE Handle GetMousePickSystem(void) const noexcept {return mousePickSystem;}; Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;}; Handle GetPrimaryRenderpass() const noexcept; - //SHRenderGraph const& GetRenderGraph(void) const noexcept; - - //Handle GetRenderPass() const { return renderPass; } + Handle GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; } + uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } /*-----------------------------------------------------------------------------*/ /* Getters */ @@ -339,10 +338,6 @@ namespace SHADE Handle worldViewport; // Whole screen std::vector> viewports; // Additional viewports - // Debug Renderers - Handle debugWorldRenderer; - Handle debugScreenRenderer; - // Temp renderers Handle worldRenderer; @@ -353,10 +348,13 @@ namespace SHADE // Built-In Shaders Handle defaultVertShader; Handle defaultFragShader; + Handle debugVertShader; + Handle debugFragShader; Handle deferredCompositeShader; // Built-In Materials Handle defaultMaterial; + Handle debugDrawPipeline; Handle worldRenderGraph; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index be181beb..c9a3f6c5 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 = 12; + const int LONG_SLICES = 12; + 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 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/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/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/Transform/SHTransformSystem.cpp b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp index de42121d..f000aa5b 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransformSystem.cpp @@ -261,7 +261,7 @@ namespace SHADE auto* node = EVENT_DATA->data->node; auto* tf = SHComponentManager::GetComponent_s(node->GetEntityID()); - if(tf == nullptr) + if (tf == nullptr) return EVENT_DATA->handle; // Recompute local transform and store localToWorld Matrix 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..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; @@ -460,13 +461,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..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 */ @@ -134,6 +136,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; }; 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 diff --git a/TempShaderFolder/DeferredCompositeCs.glsl b/TempShaderFolder/DeferredCompositeCs.glsl deleted file mode 100644 index c1caf0aa..00000000 --- a/TempShaderFolder/DeferredCompositeCs.glsl +++ /dev/null @@ -1,83 +0,0 @@ -#version 450 - -struct DirectionalLightStruct -{ - vec3 direction; - uint isActive; - uint cullingMask; - vec4 diffuseColor; -}; - -struct AmbientLightStruct -{ - vec4 ambientColor; - float strength; - uint isActive; - uint cullingMask; -}; - -layout(local_size_x = 16, local_size_y = 16) in; -layout(set = 4, binding = 0, rgba32f) uniform image2D positions; -layout(set = 4, binding = 1, rgba32f) uniform image2D normals; -layout(set = 4, binding = 2, rgba8) uniform image2D albedo; -layout(set = 4, binding = 3, r32ui) uniform uimage2D lightLayerData; -layout(set = 4, binding = 4, rgba8) uniform image2D targetImage; - -layout(set = 1, binding = 0) uniform LightCounts -{ - uint directionalLights; - uint pointLights; - uint spotLights; - uint ambientLights; - -} lightCounts; - -layout(std430, set = 1, binding = 1) buffer DirectionalLightData -{ - DirectionalLightStruct dLightData[]; -} DirLightData; - -layout(std430, set = 1, binding = 4) buffer AmbientLightData -{ - AmbientLightStruct aLightData[]; -} AmbLightData; - -void main() -{ - // convenient variables - ivec2 globalThread = ivec2(gl_GlobalInvocationID); - - // Get the diffuse color of the pixel - vec3 pixelDiffuse = imageLoad (albedo, globalThread).rgb; - - // Get position of fragment in world space - vec3 positionWorld = imageLoad (positions, globalThread).rgb; - - // normal of fragment - vec3 normalWorld = imageLoad(normals, globalThread).rgb; - - vec3 fragColor = vec3 (0.0f); - - for (int i = 0; i < lightCounts.directionalLights; ++i) - { - // get normalized direction of light - vec3 dLightNormalized = normalize (DirLightData.dLightData[i].direction); - - // Get diffuse strength - float diffuseStrength = max (0, dot (dLightNormalized, normalWorld)); - - // Calculate the fragment color - fragColor += DirLightData.dLightData[i].diffuseColor.rgb * diffuseStrength.rrr * pixelDiffuse; - } - - for (int i = 0; i < lightCounts.ambientLights; ++i) - { - // Just do some add - //fragColor += pixelDiffuse.rgb * AmbLightData.aLightData[i].ambientColor.rgb * vec3 (0.5f); - fragColor += pixelDiffuse.rgb * AmbLightData.aLightData[i].ambientColor.rgb * vec3 (AmbLightData.aLightData[i].strength); - } - - // store result into result image - imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(fragColor, 1.0f)); - -} \ No newline at end of file diff --git a/TempShaderFolder/DeferredCompositeCs.spv b/TempShaderFolder/DeferredCompositeCs.spv deleted file mode 100644 index 03ef7ac5..00000000 Binary files a/TempShaderFolder/DeferredCompositeCs.spv and /dev/null differ diff --git a/TempShaderFolder/TestCubeFs.spv b/TempShaderFolder/TestCubeFs.spv deleted file mode 100644 index 2381b834..00000000 Binary files a/TempShaderFolder/TestCubeFs.spv and /dev/null differ diff --git a/TempShaderFolder/TestCubeVs.spv b/TempShaderFolder/TestCubeVs.spv deleted file mode 100644 index d8c3c3d0..00000000 Binary files a/TempShaderFolder/TestCubeVs.spv and /dev/null differ