From 85a63cec1fc8f734317699c62cfe97563872fbc0 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 8 Sep 2022 12:06:29 +0800 Subject: [PATCH 01/36] #comment removed two CreateEntity function overloads and added a default value to desired EID instead --- .../ECS_Base/System/SHEntityManager.cpp | 65 +++------------- .../Engine/ECS_Base/System/SHEntityManager.h | 75 ++++--------------- 2 files changed, 24 insertions(+), 116 deletions(-) diff --git a/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.cpp b/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.cpp index 4aa38112..240c0b32 100644 --- a/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.cpp +++ b/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.cpp @@ -52,63 +52,20 @@ namespace SHADE return entityHandle.GetIndex(entityID); } - EntityID SHEntityManager::CreateEntity(std::vectorconst& componentTypeIDs, std::string const& name,EntityID parentEID) - { - EntityID eID = entityHandle.GetNewHandle(); - EntityIndex eIndex = entityHandle.GetIndex(eID); - if (eIndex > entityVec.size()) - { - assert("FATAL ERROR: EntityIndex out of range in Entity Creation"); - } - else if (eIndex == entityVec.size()) - { - entityVec.emplace_back(std::make_unique()); - } - else - { - if (!entityVec[eIndex]) - { - //There is still an entity stored there.Something went wrong - assert("FATAL ERROR: Entity Creation error. Entity Index Conflict"); - } - - //Reset it to a newly constructed entity - entityVec[eIndex].reset(new SHEntity()); - } - - - entityVec[eIndex]->entityID = eID; - entityVec[eIndex]->name = name; - for (auto& id : componentTypeIDs) - { - SHComponentManager::AddComponent(eID, id); - } - - //(SHComponentManager::AddComponent(eID), ...); - /*if (entityHandle.IsValid(parentEID) == false) - { - entityVec[eIndex]->sceneNode.ConnectToRoot(); - } - else - { - entityVec[eIndex]->SetParent(parentEID); - }*/ - - - //TODO Link to Scene graph. - - return eID; - - } - EntityID SHEntityManager::CreateEntity(std::vectorconst& componentTypeIDs, EntityID desiredEID, std::string const& name, EntityID parentEID) { - EntityID eID ; - - if (entityHandle.ClaimHandle(desiredEID) == true) - eID = desiredEID; - else + EntityID eID; + if (desiredEID == MAX_EID) + { eID = entityHandle.GetNewHandle(); + } + else + { + if (entityHandle.ClaimHandle(desiredEID) == true) + eID = desiredEID; + else + eID = entityHandle.GetNewHandle(); + } EntityIndex eIndex = entityHandle.GetIndex(eID); diff --git a/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.h b/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.h index 11e896d5..7afece52 100644 --- a/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.h +++ b/SHADE_Engine/src/Engine/ECS_Base/System/SHEntityManager.h @@ -81,55 +81,21 @@ namespace SHADE * EntityID of the new Entity ***************************************************************************/ template - static std::enable_if_t<(... && std::is_base_of_v), EntityID> CreateEntity(std::string const& name = "Default", EntityID parentEID = MAX_EID) - { - EntityID eID = entityHandle.GetNewHandle(); - EntityIndex eIndex = entityHandle.GetIndex(eID); - if (eIndex > entityVec.size()) - { - assert("FATAL ERROR: EntityIndex out of range in Entity Creation"); - } - else if (eIndex == entityVec.size()) - { - entityVec.emplace_back(std::make_unique()); - } - else - { - if (!entityVec[eIndex]) - { - //There is still an entity stored there.Something went wrong - assert("FATAL ERROR: Entity Creation error. Entity Index Conflict"); - } - - //Reset it to a newly constructed entity - entityVec[eIndex].reset(new SHEntity()); - } - entityVec[eIndex]->entityID = eID; - entityVec[eIndex]->name = name; - (SHComponentManager::AddComponent(eID),...); - - /*if (entityHandle.IsValid(parentEID) == false) - { - entityVec[eIndex]->sceneNode.ConnectToRoot(); - } - else - { - entityVec[eIndex]->SetParent(parentEID); - }*/ - - //TODO Link up with Scene graph - - return eID; - } - - template - static std::enable_if_t<(... && std::is_base_of_v), EntityID> CreateEntity(EntityID desiredEID, std::string const& name = "Default", EntityID parentEID = MAX_EID) + static std::enable_if_t<(... && std::is_base_of_v), EntityID> CreateEntity(EntityID desiredEID = MAX_EID, std::string const& name = "Default", EntityID parentEID = MAX_EID) { EntityID eID; - if (entityHandle.ClaimHandle(desiredEID) == true) - eID = desiredEID; - else + if (desiredEID == MAX_EID) + { eID = entityHandle.GetNewHandle(); + } + else + { + if (entityHandle.ClaimHandle(desiredEID) == true) + eID = desiredEID; + else + eID = entityHandle.GetNewHandle(); + } + EntityIndex eIndex = entityHandle.GetIndex(eID); if (eIndex > entityVec.size()) { @@ -179,21 +145,6 @@ namespace SHADE - /************************************************************************** - * \brief - * Create Entity using a vector of ComponentTypeIDs. - * \param componentTypeIDs - * Vector of ComponentTypeIDs. This assumes that CreateSparseSet is called - * for these ComponentTypes. - * \param name - * Name of the Entity (this is not unique) - * \param parentEID - * The entity ID of the parent. This does not call UpdateHierarchy hence - * the parent of the entity is not updated until UpdateHierarchy is called. - * \return - * EntityID of the new Entity - ***************************************************************************/ - static EntityID CreateEntity(std::vectorconst& componentTypeIDs,std::string const& name = "Default", EntityID parentEID = MAX_EID); /************************************************************************** * \brief @@ -209,7 +160,7 @@ namespace SHADE * \return * EntityID of the new Entity ***************************************************************************/ - static EntityID CreateEntity(std::vectorconst& componentTypeIDs, EntityID desiredEID, std::string const& name = "Default", EntityID parentEID = MAX_EID); + static EntityID CreateEntity(std::vectorconst& componentTypeIDs, EntityID desiredEID = MAX_EID, std::string const& name = "Default", EntityID parentEID = MAX_EID); /************************************************************************** * \brief From 32dc894608e8ee9f6e5186cf55ac1244650be727 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Thu, 8 Sep 2022 19:37:49 +0800 Subject: [PATCH 02/36] SHEditor Base --- SHADE_Engine/src/Editor/SHEditor.cpp | 62 +++++++++++++++++++++++ SHADE_Engine/src/Editor/SHEditor.h | 25 +++++++++ SHADE_Engine/src/Editor/SHEditorBackend.h | 3 ++ 3 files changed, 90 insertions(+) create mode 100644 SHADE_Engine/src/Editor/SHEditor.cpp create mode 100644 SHADE_Engine/src/Editor/SHEditor.h create mode 100644 SHADE_Engine/src/Editor/SHEditorBackend.h diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp new file mode 100644 index 00000000..17b09291 --- /dev/null +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -0,0 +1,62 @@ +#include "SHpch.h" +#include "SHEditor.h" +#include "SHEditorBackend.h" + +namespace SHADE +{ + void SHEditor::Initialize() + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + + SetupWin32Backend(); + SetupVulkanBackend(); + + } + + void SHEditor::Update() + { + NewFrame(); + + //Add all windows to draw list, Perform necessary updates + + ImGui::Render(); + } + + void SHEditor::Render() + { + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), ) + } + + void SHEditor::Exit() + { + ShutdownVulkanBackend(); + ShutdownWin32Backend(); + ImGui::DestroyContext(); + } + + void SHEditor::SetupVulkanBackend() + { + ImGui_ImplVulkan_InitInfo initInfo; + + ImGui_ImplVulkan_Init(&initInfo, VK_NULL_HANDLE); + } + + void SHEditor::ShutdownVulkanBackend() + { + ImGui_ImplVulkan_Shutdown(); + } + + void SHEditor::NewFrame() + { + } + + void SHEditor::SetupWin32Backend() + { + //ImGui_ImplWin32_Init(/*hwnd*/); + } + + void SHEditor::ShutdownWin32Backend() + { + } +} diff --git a/SHADE_Engine/src/Editor/SHEditor.h b/SHADE_Engine/src/Editor/SHEditor.h new file mode 100644 index 00000000..a3b03421 --- /dev/null +++ b/SHADE_Engine/src/Editor/SHEditor.h @@ -0,0 +1,25 @@ +#pragma once + +namespace SHADE +{ + class SHEditor + { + public: + + static void Initialize(); + + static void Update(); + + static void Render(); + + static void Exit(); + + private: + static void SetupWin32Backend(); + static void ShutdownWin32Backend(); + static void SetupVulkanBackend(); + static void ShutdownVulkanBackend(); + + static void NewFrame(); + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditorBackend.h b/SHADE_Engine/src/Editor/SHEditorBackend.h new file mode 100644 index 00000000..29a19f86 --- /dev/null +++ b/SHADE_Engine/src/Editor/SHEditorBackend.h @@ -0,0 +1,3 @@ +#pragma once +#include +#include \ No newline at end of file From 28ab89cf91ea04a3d47cb6770266e578e10248d8 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Thu, 8 Sep 2022 20:34:44 +0800 Subject: [PATCH 03/36] set up backend functions --- SHADE_Engine/src/Editor/SHEditor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index 17b09291..79043aeb 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -25,7 +25,7 @@ namespace SHADE void SHEditor::Render() { - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), ) + //ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), ) } void SHEditor::Exit() @@ -38,7 +38,7 @@ namespace SHADE void SHEditor::SetupVulkanBackend() { ImGui_ImplVulkan_InitInfo initInfo; - + //initInfo.Device = ImGui_ImplVulkan_Init(&initInfo, VK_NULL_HANDLE); } From cecbca26c5c94bf563535e85512a29083d4e3d41 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 8 Sep 2022 20:55:56 +0800 Subject: [PATCH 04/36] Fixed some warnings in ResourceLibrary and SparseSet --- SHADE_Engine/src/Resource/Handle.h | 2 +- SHADE_Engine/src/Resource/ResourceLibrary.h | 2 +- SHADE_Engine/src/Resource/ResourceLibrary.hpp | 8 ++++---- SHADE_Engine/src/Resource/SparseSet.hpp | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/SHADE_Engine/src/Resource/Handle.h b/SHADE_Engine/src/Resource/Handle.h index 34662112..afaec8e1 100644 --- a/SHADE_Engine/src/Resource/Handle.h +++ b/SHADE_Engine/src/Resource/Handle.h @@ -139,7 +139,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Data Members */ /*-----------------------------------------------------------------------------*/ - ResourceLibrary* library; + ResourceLibrary* library = nullptr; /*-----------------------------------------------------------------------------*/ /* Friend Declarations */ diff --git a/SHADE_Engine/src/Resource/ResourceLibrary.h b/SHADE_Engine/src/Resource/ResourceLibrary.h index b05dc499..01a78d16 100644 --- a/SHADE_Engine/src/Resource/ResourceLibrary.h +++ b/SHADE_Engine/src/Resource/ResourceLibrary.h @@ -69,7 +69,7 @@ namespace SHADE /* Helper Functions */ /*-----------------------------------------------------------------------------*/ void assertHandleValid(Handle handle) const; - int getAvailableFreeIndex(); + uint32_t getAvailableFreeIndex(); }; /// diff --git a/SHADE_Engine/src/Resource/ResourceLibrary.hpp b/SHADE_Engine/src/Resource/ResourceLibrary.hpp index 2ad856c6..9aa72ead 100644 --- a/SHADE_Engine/src/Resource/ResourceLibrary.hpp +++ b/SHADE_Engine/src/Resource/ResourceLibrary.hpp @@ -35,13 +35,13 @@ namespace SHADE } else { - handle.id.Data.Version = 0; - versionCounts.insert(handle.id.Data.Index, handle.id.Data.Version); + handle.id.Data.Version = 0U; + versionCounts.insert(static_cast(handle.id.Data.Index), handle.id.Data.Version); } handle.library = this; // Create the resource - [[maybe_unused]] T& obj = objects.insert(handle.id.Data.Index, std::forward(args) ...); + [[maybe_unused]] T& obj = objects.insert(static_cast(handle.id.Data.Index), std::forward(args) ...); // Handling SelfHandle assignment if constexpr (std::is_base_of_v, T>) @@ -89,7 +89,7 @@ namespace SHADE } template - inline int ResourceLibrary::getAvailableFreeIndex() + inline uint32_t ResourceLibrary::getAvailableFreeIndex() { // Get from the free list if present if (!freeList.empty()) diff --git a/SHADE_Engine/src/Resource/SparseSet.hpp b/SHADE_Engine/src/Resource/SparseSet.hpp index 70e15f29..ec89790b 100644 --- a/SHADE_Engine/src/Resource/SparseSet.hpp +++ b/SHADE_Engine/src/Resource/SparseSet.hpp @@ -56,7 +56,7 @@ namespace SHADE throw std::invalid_argument("An element at this index does not exist!"); // Swap with the last element - const int BACK_IDX = denseArray.size() - 1; + const auto BACK_IDX = denseArray.size() - 1; std::swap(denseArray[sparseArray[idx]], denseArray.back()); denseArray.pop_back(); // Update the sparse set by swapping the indices @@ -110,7 +110,7 @@ namespace SHADE // We need to resize the array if (idx >= sparseArray.size()) { - const int NEW_SIZE = idx + 1; + const auto NEW_SIZE = idx + 1; sparseArray.resize(NEW_SIZE, INVALID); inverseSparseArray.resize(NEW_SIZE, INVALID); } @@ -123,7 +123,7 @@ namespace SHADE auto& insertedElem = denseArray.emplace_back(std::forward(args) ...); // Update sparse and inverse sparse arrays - const index_type DENSE_IDX = denseArray.size() - 1; + const auto DENSE_IDX = denseArray.size() - 1; sparseArray[idx] = DENSE_IDX; inverseSparseArray[DENSE_IDX] = idx; From fe6c5be8c60ad0aa82118a2e1d2d3adae208b3f6 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 11:20:38 +0800 Subject: [PATCH 05/36] Graphics System called in application --- SHADE_Application/SHADE_Application.vcxproj | 8 +++--- SHADE_Application/premake5.lua | 8 ++++++ .../src/Application/SBApplication.cpp | 8 ++++++ .../src/Application/SBApplication.h | 1 + .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 25 +++++++++++++------ .../MiddleEnd/Interface/SHGraphicsSystem.h | 19 +++++++++++--- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/SHADE_Application/SHADE_Application.vcxproj b/SHADE_Application/SHADE_Application.vcxproj index 11bbb48f..ab1ac84c 100644 --- a/SHADE_Application/SHADE_Application.vcxproj +++ b/SHADE_Application/SHADE_Application.vcxproj @@ -59,8 +59,8 @@ Use SBpch.h Level4 - _DEBUG;%(PreprocessorDefinitions) - ..\Dependencies\spdlog\include;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) + NOMINMAX;_DEBUG;%(PreprocessorDefinitions) + ..\Dependencies\spdlog\include;$(VULKAN_SDK)\include;..\Dependencies\VMA\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) EditAndContinue Disabled false @@ -79,8 +79,8 @@ Use SBpch.h Level4 - _RELEASE;%(PreprocessorDefinitions) - ..\Dependencies\spdlog\include;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) + NOMINMAX;_RELEASE;%(PreprocessorDefinitions) + ..\Dependencies\spdlog\include;$(VULKAN_SDK)\include;..\Dependencies\VMA\include;$(VULKAN_SDK)\Source\SPIRV-Reflect;..\SHADE_Engine\src;src;%(AdditionalIncludeDirectories) Full true true diff --git a/SHADE_Application/premake5.lua b/SHADE_Application/premake5.lua index c10b1002..4f2711e7 100644 --- a/SHADE_Application/premake5.lua +++ b/SHADE_Application/premake5.lua @@ -22,6 +22,9 @@ project "SHADE_Application" includedirs { "%{IncludeDir.spdlog}/include", + "%{IncludeDir.VULKAN}/include", + "%{IncludeDir.VMA}/include", + "%{IncludeDir.VULKAN}/Source/SPIRV-Reflect", "../SHADE_Engine/src", "src" } @@ -39,6 +42,11 @@ project "SHADE_Application" postbuildcommands { } + + defines + { + "NOMINMAX" + } warnings 'Extra' diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 184b9611..2a4a18af 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -1,5 +1,6 @@ #include "SBpch.h" #include "SBApplication.h" +#include "Engine/ECS_Base/System/SHSystemManager.h" #ifdef SHEDITOR #include "Editor/SHEditor.h" @@ -23,6 +24,11 @@ namespace Sandbox { window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow); + SHADE::SHSystemManager::CreateSystem("Graphics System"); + SHADE::SHGraphicsSystem* graphicsSystem = static_cast(SHADE::SHSystemManager::GetSystem("Graphics System")); + graphicsSystem->SetWindow(&window); + + SHADE::SHSystemManager::Init(); #ifdef SHEDITOR #else @@ -44,6 +50,8 @@ namespace Sandbox void SBApplication::Exit(void) { + SHADE::SHSystemManager::Exit(); + #ifdef SHEDITOR #else #endif diff --git a/SHADE_Application/src/Application/SBApplication.h b/SHADE_Application/src/Application/SBApplication.h index a1bf11eb..94e296b1 100644 --- a/SHADE_Application/src/Application/SBApplication.h +++ b/SHADE_Application/src/Application/SBApplication.h @@ -1,6 +1,7 @@ #ifndef SB_APPLICATION_H #define SB_APPLICATION_H #include +#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" //using namespace SHADE; namespace Sandbox diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index ad70a18f..7bc2f9ee 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -25,11 +25,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*---------------------------------------------------------------------------------*/ - SHGraphicsSystem::SHGraphicsSystem(SHWindow& window) + void SHGraphicsSystem::Init(void) { - // Save the SHWindow - this->window = &window; - // Set Up Instance SHVkInstance::Init(true, true, true); @@ -39,10 +36,10 @@ namespace SHADE device = SHVkInstance::CreateLogicalDevice({ SHQueueParams(SH_Q_FAM::GRAPHICS, SH_QUEUE_SELECT::DEDICATED), SHQueueParams(SH_Q_FAM::TRANSFER, SH_QUEUE_SELECT::DEDICATED) }, physicalDevice); // Construct surface - surface = device->CreateSurface(window.GetHWND()); + surface = device->CreateSurface(window->GetHWND()); // Construct Swapchain - auto windowDims = window.GetWindowSize(); + auto windowDims = window->GetWindowSize(); swapchain = device->CreateSwapchain(surface, windowDims.first, windowDims.second, SHSwapchainParams { .surfaceImageFormats {vk::Format::eB8G8R8A8Unorm, vk::Format::eR8G8B8A8Unorm, vk::Format::eB8G8R8Unorm, vk::Format::eR8G8B8Unorm}, @@ -51,7 +48,7 @@ namespace SHADE .vsyncOn = false, // TODO: Set to true when shipping game }); - window.RegisterWindowSizeCallback([&]([[maybe_unused]] uint32_t width, [[maybe_unused]] uint32_t height) + window->RegisterWindowSizeCallback([&]([[maybe_unused]] uint32_t width, [[maybe_unused]] uint32_t height) { renderContext.SetIsResized(true); }); @@ -127,7 +124,13 @@ namespace SHADE /* RENDERGRAPH END TESTING */ /*-----------------------------------------------------------------------*/ } - SHGraphicsSystem::~SHGraphicsSystem() + + void SHGraphicsSystem::Run(float dt) + { + + } + + void SHGraphicsSystem::Exit(void) { //renderPass.Free(); renderContext.Destroy(); @@ -235,4 +238,10 @@ namespace SHADE void SHGraphicsSystem::RemoveSegment(Handle segment) { } + + void SHGraphicsSystem::SetWindow(SHWindow* wind) noexcept + { + window = wind; + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index e1dff379..322ad98f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -21,6 +21,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/SHVulkanIncludes.h" #include "Graphics/MiddleEnd/PerFrame/SHRenderContext.h" #include "Graphics/RenderGraph/SHRenderGraph.h" +#include "Engine/ECS_Base/System/SHSystem.h" namespace SHADE { @@ -61,7 +62,7 @@ namespace SHADE portions of the screen. */ /***********************************************************************************/ - class SHGraphicsSystem + class SHGraphicsSystem : public SHSystem { public: /*-----------------------------------------------------------------------------*/ @@ -72,8 +73,15 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*-----------------------------------------------------------------------------*/ - SHGraphicsSystem(SHWindow& window); - ~SHGraphicsSystem(); + SHGraphicsSystem (void) = default; + ~SHGraphicsSystem(void) noexcept = default; + + /*-----------------------------------------------------------------------------*/ + /* SHSystem overrides */ + /*-----------------------------------------------------------------------------*/ + virtual void Init(void) override; + virtual void Run (float dt) override; + virtual void Exit(void) override; /*-----------------------------------------------------------------------------*/ /* Lifecycle Functions */ @@ -93,6 +101,11 @@ namespace SHADE Handle AddSegment(const VkViewport& viewport, Handle imageToUse); void RemoveSegment(Handle segment); + /*-----------------------------------------------------------------------------*/ + /* Setters */ + /*-----------------------------------------------------------------------------*/ + void SetWindow (SHWindow* wind) noexcept; + /*-----------------------------------------------------------------------------*/ /* Getters (Temporary) */ /*-----------------------------------------------------------------------------*/ From bed017e5399de35045d7b2de38a2383cb9250b3e Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 13:28:57 +0800 Subject: [PATCH 06/36] Fixed some warnings and premake files now include hpp files --- SHADE_Application/premake5.lua | 3 ++- SHADE_Engine/SHADE_Engine.vcxproj | 3 +++ SHADE_Engine/SHADE_Engine.vcxproj.filters | 9 +++++++++ SHADE_Engine/premake5.lua | 4 ++-- .../src/Graphics/Devices/SHVkPhysicalDeviceLibrary.cpp | 4 ++-- SHADE_Engine/src/Graphics/Swapchain/SHVkSwapchain.cpp | 4 ++++ SHADE_Engine/src/Resource/SparseSet.hpp | 2 +- 7 files changed, 23 insertions(+), 6 deletions(-) diff --git a/SHADE_Application/premake5.lua b/SHADE_Application/premake5.lua index 4f2711e7..74a3d0de 100644 --- a/SHADE_Application/premake5.lua +++ b/SHADE_Application/premake5.lua @@ -14,7 +14,8 @@ project "SHADE_Application" files { "%{prj.location}/src/**.h", - "%{prj.location}/src/**.c", + "%{prj.location}/src/**.hpp", + "%{prj.location}/src/**.c", "%{prj.location}/src/**.cpp", "%{prj.location}/src/**.glsl", } diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 71ffb25f..3d1c506f 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -173,8 +173,11 @@ + + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index f38a78a9..ceb9b80f 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -321,12 +321,21 @@ Resource + + Resource + Resource + + Resource + Resource + + Resource + Scene diff --git a/SHADE_Engine/premake5.lua b/SHADE_Engine/premake5.lua index a94284df..fbc800d3 100644 --- a/SHADE_Engine/premake5.lua +++ b/SHADE_Engine/premake5.lua @@ -12,10 +12,10 @@ project "SHADE_Engine" files { "%{prj.location}/src/**.h", + "%{prj.location}/src/**.hpp", "%{prj.location}/src/**.c", "%{prj.location}/src/**.cpp", - "%{prj.location}/src/**.glsl", - "%{wks.location}/Dependencies/stb_image/**.cpp" + "%{prj.location}/src/**.glsl" } includedirs diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkPhysicalDeviceLibrary.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkPhysicalDeviceLibrary.cpp index 511af8fc..d815fb7e 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkPhysicalDeviceLibrary.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkPhysicalDeviceLibrary.cpp @@ -180,10 +180,10 @@ namespace SHADE return; } - SHLOG_ERROR("Successfully queried Physical Devices:"); + SHLOG_TRACE("Successfully queried Physical Devices:"); for (auto const& device : physicalDevices) { - SHLOG_ERROR(std::string_view (std::string("\t-") + GetDeviceTypeName(device.getProperties().deviceType) + device.getProperties().deviceName.operator std::string())); + SHLOG_TRACE(std::string_view (std::string("\t-") + GetDeviceTypeName(device.getProperties().deviceType) + device.getProperties().deviceName.operator std::string())); } } } diff --git a/SHADE_Engine/src/Graphics/Swapchain/SHVkSwapchain.cpp b/SHADE_Engine/src/Graphics/Swapchain/SHVkSwapchain.cpp index 4dd6f9d6..f46d5d17 100644 --- a/SHADE_Engine/src/Graphics/Swapchain/SHVkSwapchain.cpp +++ b/SHADE_Engine/src/Graphics/Swapchain/SHVkSwapchain.cpp @@ -19,10 +19,14 @@ namespace SHADE vkPresentModes = physicalDeviceHdl->GetVkPhysicalDevice().getSurfacePresentModesKHR(surfaceHdl->GetVkSurface()); if (vkSurfaceFormats.size() == 0) + { SHLOG_ERROR("Failed to get surface formats from the physical device. "); + } if (vkPresentModes.size() == 0) + { SHLOG_ERROR("Failed to get present modes from the physical device. "); + } } vk::SurfaceFormatKHR SHVkSwapchain::ChooseSwapSurfaceFormat(std::vector const& surfaceFormats) const noexcept diff --git a/SHADE_Engine/src/Resource/SparseSet.hpp b/SHADE_Engine/src/Resource/SparseSet.hpp index ec89790b..5afcdee7 100644 --- a/SHADE_Engine/src/Resource/SparseSet.hpp +++ b/SHADE_Engine/src/Resource/SparseSet.hpp @@ -123,7 +123,7 @@ namespace SHADE auto& insertedElem = denseArray.emplace_back(std::forward(args) ...); // Update sparse and inverse sparse arrays - const auto DENSE_IDX = denseArray.size() - 1; + const auto DENSE_IDX = static_cast(denseArray.size()) - 1; sparseArray[idx] = DENSE_IDX; inverseSparseArray[DENSE_IDX] = idx; From 94a358995a07b7c2ca8e937f2669b83dea73a164 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 13:32:55 +0800 Subject: [PATCH 07/36] Window is rendering black screen --- SHADE_Application/src/Application/SBApplication.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 2a4a18af..69662415 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -38,13 +38,17 @@ namespace Sandbox void SBApplication::Update(void) { + SHADE::SHGraphicsSystem* graphicsSystem = static_cast(SHADE::SHSystemManager::GetSystem("Graphics System")); + //TODO: Change true to window is open while (!window.WindowShouldClose()) { + graphicsSystem->BeginRender(); #ifdef SHEDITOR #else #endif - } + graphicsSystem->EndRender(); + } } From 78f8a9e4559c25daef4dda9e9c8a98a61e25588d Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 13:55:00 +0800 Subject: [PATCH 08/36] Getter for physical device --- SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h | 1 + 1 file changed, 1 insertion(+) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 322ad98f..9b867744 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -112,6 +112,7 @@ namespace SHADE Handle GetDevice() const { return device; } Handle GetSwapchain() const { return swapchain; } Handle GetSurface() const { return surface; } + Handle GetPhysicalDevice() const {return physicalDevice;} //Handle GetRenderPass() const { return renderPass; } From fe954271cba7efcfd46ec9a6945e86acd8c94927 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 14:02:48 +0800 Subject: [PATCH 09/36] Getter for queue --- SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h | 1 + 1 file changed, 1 insertion(+) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 9b867744..c3c94a6f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -113,6 +113,7 @@ namespace SHADE Handle GetSwapchain() const { return swapchain; } Handle GetSurface() const { return surface; } Handle GetPhysicalDevice() const {return physicalDevice;} + Handle GetQueue () const {return queue;} //Handle GetRenderPass() const { return renderPass; } From d7954245d66dd84e6d43c88125eeca833b28c434 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 14:21:13 +0800 Subject: [PATCH 10/36] Created a descriptor pool for Graphics System --- .../Descriptors/SHVkDescriptorPool.cpp | 70 ++++++++++++------- .../Graphics/Descriptors/SHVkDescriptorPool.h | 4 +- .../Graphics/Devices/SHVkLogicalDevice.cpp | 6 ++ .../src/Graphics/Devices/SHVkLogicalDevice.h | 3 +- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 2 + .../MiddleEnd/Interface/SHGraphicsSystem.h | 5 +- 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp index 87d43255..77663ab8 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp @@ -7,32 +7,52 @@ namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Constructor/Destructor */ - /*---------------------------------------------------------------------------------*/ - SHVkDescriptorPool::SHVkDescriptorPool(Handle device, const Config& config) - : device { device } + /*---------------------------------------------------------------------------------*/ + /* Constructor/Destructor */ + /*---------------------------------------------------------------------------------*/ + SHVkDescriptorPool::SHVkDescriptorPool(Handle device, const Config& config) + : device{ device } + { + // Create the Pool + const vk::DescriptorPoolCreateInfo POOL_CREATE_INFO { - // Create the Pool - const vk::DescriptorPoolCreateInfo POOL_CREATE_INFO - { - .flags = config.Flags, - .maxSets = config.MaxSets, - .poolSizeCount = static_cast(config.Limits.size()), - .pPoolSizes = config.Limits.data() - }; - pool = device->GetVkLogicalDevice().createDescriptorPool(POOL_CREATE_INFO); - } + .flags = config.Flags, + .maxSets = config.MaxSets, + .poolSizeCount = static_cast(config.Limits.size()), + .pPoolSizes = config.Limits.data() + }; + pool = device->GetVkLogicalDevice().createDescriptorPool(POOL_CREATE_INFO); + } - SHVkDescriptorPool::~SHVkDescriptorPool() noexcept - { - if (pool) - device->GetVkLogicalDevice().destroyDescriptorPool(pool); - } + SHVkDescriptorPool::SHVkDescriptorPool(SHVkDescriptorPool&& rhs) noexcept + : device{ rhs.device } + , pool{ rhs.pool } + { + rhs.pool = VK_NULL_HANDLE; + } - std::vector> SHVkDescriptorPool::Allocate(const std::vector>& layouts, std::vector const& variableDescCounts) - { - SHVkInstance::GetResourceManager().Create(device, GetHandle(), layouts, variableDescCounts); - return {}; - } + SHVkDescriptorPool::~SHVkDescriptorPool() noexcept + { + if (pool) + device->GetVkLogicalDevice().destroyDescriptorPool(pool); + } + + SHVkDescriptorPool& SHVkDescriptorPool::operator=(SHVkDescriptorPool&& rhs) noexcept + { + if (&rhs == this) + return *this; + + device = rhs.device; + pool = rhs.pool; + + rhs.pool = VK_NULL_HANDLE; + + return *this; + } + + std::vector> SHVkDescriptorPool::Allocate(const std::vector>& layouts, std::vector const& variableDescCounts) + { + SHVkInstance::GetResourceManager().Create(device, GetHandle(), layouts, variableDescCounts); + return {}; + } } diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h index c3059b8b..9314d940 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h @@ -65,7 +65,7 @@ namespace SHADE /// SHVkDescriptorPool(Handle device, const Config& config = {}); SHVkDescriptorPool(const SHVkDescriptorPool&) = delete; - SHVkDescriptorPool(SHVkDescriptorPool&& rhs) noexcept = default; + SHVkDescriptorPool(SHVkDescriptorPool&& rhs) noexcept; /// /// Destructor which will unload and deallocate all resources for this Pool. /// @@ -75,7 +75,7 @@ namespace SHADE /* Overloaded Operators */ /*-----------------------------------------------------------------------------*/ SHVkDescriptorPool& operator=(const SHVkDescriptorPool&) = delete; - SHVkDescriptorPool& operator=(SHVkDescriptorPool&& rhs) noexcept = default; + SHVkDescriptorPool& operator=(SHVkDescriptorPool&& rhs) noexcept; /*-----------------------------------------------------------------------------*/ /* Getter Functions */ diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index aa442805..0fa1c864 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -509,6 +509,12 @@ namespace SHADE } + Handle SHVkLogicalDevice::CreateDescriptorPools(const SHVkDescriptorPool::Config& config /*= {}*/) noexcept + { + return SHVkInstance::GetResourceManager().Create (GetHandle(), config); + + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index b8eec993..78108b94 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -17,7 +17,7 @@ #include "Graphics/Pipeline/SHPipelineState.h" #include "Graphics/Pipeline/SHPipelineType.h" #include "vk_mem_alloc.h" -//#include "Graphics/DescriptorSets/SHDescriptorPool.h" +#include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" namespace SHADE @@ -165,6 +165,7 @@ namespace SHADE Handle CreateRenderpass (std::span const vkDescriptions, std::span const spDescs, std::span const spDeps) noexcept; Handle CreateFramebuffer (Handle const& renderpassHdl, std::vector> const& attachments, uint32_t inWidth, uint32_t inHeight) noexcept; Handle CreateDescriptorSetLayout (std::vector const& bindings) noexcept; + Handle CreateDescriptorPools (const SHVkDescriptorPool::Config& config = {}) noexcept; Handle CreatePipelineLayout (SHPipelineLayoutParams& pipelineLayoutParams) noexcept; Handle CreateFence (void) const noexcept; Handle CreateSemaphore (void) const noexcept; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 7bc2f9ee..9c395c9d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -86,6 +86,8 @@ namespace SHADE + descPool = device->CreateDescriptorPools(); + /*-----------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index c3c94a6f..fabef927 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -22,6 +22,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/PerFrame/SHRenderContext.h" #include "Graphics/RenderGraph/SHRenderGraph.h" #include "Engine/ECS_Base/System/SHSystem.h" +#include "Graphics/Descriptors/SHVkDescriptorPool.h" namespace SHADE { @@ -113,7 +114,8 @@ namespace SHADE Handle GetSwapchain() const { return swapchain; } Handle GetSurface() const { return surface; } Handle GetPhysicalDevice() const {return physicalDevice;} - Handle GetQueue () const {return queue;} + Handle GetQueue() const { return queue; } + Handle GetDescriptorPool() const { return descPool; } //Handle GetRenderPass() const { return renderPass; } @@ -127,6 +129,7 @@ namespace SHADE Handle surface; Handle swapchain; Handle queue; + Handle descPool; //Handle renderPass; // Potentially bring out? std::vector screenSegments; SHRenderContext renderContext; From ae372f29171c403d6ad78caaa5f509a97d1ce244 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 14:34:09 +0800 Subject: [PATCH 11/36] Getter for renderpass in render node --- .../Graphics/RenderGraph/SHRenderGraph.cpp | 24 +++++++++++++++++++ .../src/Graphics/RenderGraph/SHRenderGraph.h | 10 ++++++++ 2 files changed, 34 insertions(+) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index a86913e4..68af135f 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -517,6 +517,22 @@ namespace SHADE return subpasses.at(subpassIndexing[subpassName]); } + /***************************************************************************/ + /*! + + \brief + Get the renderpass from the node. + + \return + Handle to the renderpass. + + */ + /***************************************************************************/ + Handle SHRenderGraphNode::GetRenderpass(void) const noexcept + { + return renderpass; + } + /***************************************************************************/ /*! @@ -938,4 +954,12 @@ namespace SHADE ConfigureFramebuffers(); } + Handle SHRenderGraph::GetNode(std::string const& nodeName) const noexcept + { + if (nodeIndexing.contains(nodeName)) + return nodes[nodeIndexing.at(nodeName)]; + + return {}; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index f81e84bd..dc6b7c42 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -180,6 +180,11 @@ namespace SHADE /*-----------------------------------------------------------------------*/ Handle AddSubpass (std::string subpassName) noexcept; + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + Handle GetRenderpass (void) const noexcept; + friend class SHRenderGraph; }; @@ -227,6 +232,11 @@ namespace SHADE void AddResource(std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint32_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; void Generate (void) noexcept; + + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + Handle GetNode (std::string const& nodeName) const noexcept; }; } From 361237ede16ec120c2dd8de43b2586868986c59b Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 14:38:09 +0800 Subject: [PATCH 12/36] GETTER FOR RENDER GRAPH --- .../src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp | 5 +++++ .../src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 9c395c9d..d9761adf 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -246,4 +246,9 @@ namespace SHADE window = wind; } + SHRenderGraph const& SHGraphicsSystem::GetRenderGraph(void) const noexcept + { + return renderGraph; + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index fabef927..27d03fd3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -116,6 +116,8 @@ namespace SHADE Handle GetPhysicalDevice() const {return physicalDevice;} Handle GetQueue() const { return queue; } Handle GetDescriptorPool() const { return descPool; } + SHRenderGraph const& GetRenderGraph (void) const noexcept; + //Handle GetRenderPass() const { return renderPass; } From 09c1d2654dd4271a44f6a0bc201fbdcba43f66e6 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 9 Sep 2022 16:02:34 +0800 Subject: [PATCH 13/36] WIP --- SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp | 8 ++++++++ SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index 68af135f..4ad29f73 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -954,6 +954,14 @@ namespace SHADE ConfigureFramebuffers(); } + void SHRenderGraph::Execute(void) noexcept + { + for (auto& node : nodes) + { + + } + } + Handle SHRenderGraph::GetNode(std::string const& nodeName) const noexcept { if (nodeIndexing.contains(nodeName)) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index dc6b7c42..7ba49e84 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -228,10 +228,11 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle const& logicalDevice, Handle const& swapchain) noexcept; - void AddResource(std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint32_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); - Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; - void Generate (void) noexcept; + void Init (Handle const& logicalDevice, Handle const& swapchain) noexcept; + void AddResource (std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint32_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); + Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; + void Generate (void) noexcept; + void Execute (void) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ From a8d00a29ec2c20298b4be831ee0c75b18bffc0b1 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Fri, 9 Sep 2022 16:04:49 +0800 Subject: [PATCH 14/36] [WIP] Editor: ImGui-Vulkan integration --- .../src/Application/SBApplication.cpp | 13 +++- SHADE_Engine/SHADE_Engine.vcxproj | 3 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 12 +++ SHADE_Engine/src/Editor/SHEditor.cpp | 73 +++++++++++++++++-- SHADE_Engine/src/Editor/SHEditor.h | 12 ++- 5 files changed, 100 insertions(+), 13 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 69662415..d7c71422 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -2,9 +2,10 @@ #include "SBApplication.h" #include "Engine/ECS_Base/System/SHSystemManager.h" +#define SHEDITOR #ifdef SHEDITOR #include "Editor/SHEditor.h" -#include "Scenes/SBEditorScene.h" +//#include "Scenes/SBEditorScene.h" #endif // SHEDITOR #include @@ -29,8 +30,8 @@ namespace Sandbox graphicsSystem->SetWindow(&window); SHADE::SHSystemManager::Init(); - #ifdef SHEDITOR + SHADE::SHEditor::Initialize(window.GetHWND()); #else #endif @@ -43,9 +44,14 @@ namespace Sandbox //TODO: Change true to window is open while (!window.WindowShouldClose()) { + #ifdef SHEDITOR + //SHADE::SHEditor::PreRender(); + #endif graphicsSystem->BeginRender(); #ifdef SHEDITOR - #else + SHADE::SHEditor::PreRender(); + SHADE::SHEditor::Update(); + SHADE::SHEditor::Render(); #endif graphicsSystem->EndRender(); } @@ -54,6 +60,7 @@ namespace Sandbox void SBApplication::Exit(void) { + //SHADE::SHEditor::Exit(); SHADE::SHSystemManager::Exit(); #ifdef SHEDITOR diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 3d1c506f..3d673b09 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -102,6 +102,8 @@ + + @@ -186,6 +188,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index ceb9b80f..e26cacc4 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -1,6 +1,9 @@ + + {8C1A20B0-78BC-4A86-6177-5EDA4DB8D1D6} + {DBC7D3B0-C769-FE86-B024-12DB9C6585D7} @@ -108,6 +111,12 @@ + + Editor + + + Editor + Engine\ECS_Base\Components @@ -354,6 +363,9 @@ + + Editor + Engine\ECS_Base\Components diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index 79043aeb..b3bac9d1 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -2,16 +2,41 @@ #include "SHEditor.h" #include "SHEditorBackend.h" +#include +#include + +#include +#include +#include +#include "Graphics/Swapchain/SHVkSwapchain.h" +#include "Graphics/Debugging/SHVulkanDebugUtil.h" + +#include "Tools/SHLogger.h" + namespace SHADE { - void SHEditor::Initialize() + Handle SHEditor::cmdPool; + Handle SHEditor::cmdBuffer; + + void SHEditor::Initialize(HWND hwnd) { IMGUI_CHECKVERSION(); ImGui::CreateContext(); - SetupWin32Backend(); + SetupWin32Backend(hwnd); SetupVulkanBackend(); + //ImGuiIO& io = ImGui::GetIO(); + + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + //io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + } + + void SHEditor::PreRender() + { + cmdPool->Reset(); } void SHEditor::Update() @@ -19,13 +44,21 @@ namespace SHADE NewFrame(); //Add all windows to draw list, Perform necessary updates + ImGui::ShowDemoWindow(); ImGui::Render(); } void SHEditor::Render() { - //ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), ) + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmdBuffer->GetVkCommandBuffer()); + + ImGuiIO& io = ImGui::GetIO(); + if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + } } void SHEditor::Exit() @@ -38,8 +71,31 @@ namespace SHADE void SHEditor::SetupVulkanBackend() { ImGui_ImplVulkan_InitInfo initInfo; - //initInfo.Device = - ImGui_ImplVulkan_Init(&initInfo, VK_NULL_HANDLE); + + initInfo.Instance = SHVkInstance::GetVkInstance(); + auto gfxSystem = reinterpret_cast(SHSystemManager::GetSystem("Graphics System")); + initInfo.PhysicalDevice = gfxSystem->GetPhysicalDevice()->GetVkPhysicalDevice(); + initInfo.Device = gfxSystem->GetDevice()->GetVkLogicalDevice(); + initInfo.Queue = gfxSystem->GetQueue()->GetVkQueue(); + initInfo.MinImageCount = initInfo.ImageCount = gfxSystem->GetSwapchain()->GetNumImages(); + initInfo.DescriptorPool = gfxSystem->GetDescriptorPool()->GetVkHandle(); + initInfo.MSAASamples = VK_SAMPLE_COUNT_1_BIT; + initInfo.Allocator = nullptr; + initInfo.PipelineCache = nullptr; + initInfo.Subpass = 0; + initInfo.CheckVkResultFn = [](VkResult err) + { + if (err == VK_SUCCESS) + return; + SHVulkanDebugUtil::ReportVkError(vk::Result(err), "Editor Error"); + }; + + ImGui_ImplVulkan_Init(&initInfo, gfxSystem->GetRenderGraph().GetNode("G-Buffer")->GetRenderpass()->GetVkRenderpass()); + + cmdPool = gfxSystem->GetDevice()->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); + cmdBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + + ImGui_ImplVulkan_CreateFontsTexture(cmdBuffer->GetVkCommandBuffer()); } void SHEditor::ShutdownVulkanBackend() @@ -49,11 +105,14 @@ namespace SHADE void SHEditor::NewFrame() { + ImGui_ImplWin32_NewFrame(); + ImGui_ImplVulkan_NewFrame(); + ImGui::NewFrame(); } - void SHEditor::SetupWin32Backend() + void SHEditor::SetupWin32Backend(HWND hwnd) { - //ImGui_ImplWin32_Init(/*hwnd*/); + ImGui_ImplWin32_Init(hwnd); } void SHEditor::ShutdownWin32Backend() diff --git a/SHADE_Engine/src/Editor/SHEditor.h b/SHADE_Engine/src/Editor/SHEditor.h index a3b03421..be83e22b 100644 --- a/SHADE_Engine/src/Editor/SHEditor.h +++ b/SHADE_Engine/src/Editor/SHEditor.h @@ -1,4 +1,5 @@ #pragma once +#include "Graphics/Commands/SHVkCommandPool.h" namespace SHADE { @@ -6,7 +7,9 @@ namespace SHADE { public: - static void Initialize(); + static void Initialize(HWND hwnd); + + static void PreRender(); static void Update(); @@ -15,11 +18,14 @@ namespace SHADE static void Exit(); private: - static void SetupWin32Backend(); + static void SetupWin32Backend(HWND hwnd); static void ShutdownWin32Backend(); static void SetupVulkanBackend(); static void ShutdownVulkanBackend(); static void NewFrame(); + + static Handle cmdPool; + static Handle cmdBuffer; }; -} \ No newline at end of file +} From 4b8c842615dd4708a62b55de7b5abcd1b4a0e984 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Fri, 9 Sep 2022 21:46:30 +0800 Subject: [PATCH 15/36] [WIP] SHADE ImGui Vulkan Backend --- SHADE_Engine/SHADE_Engine.vcxproj | 2 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + .../Editor/Backend/SHImGuiVulkanBackend.cpp | 613 ++++++++++++++++++ .../src/Editor/Backend/SHImGuiVulkanBackend.h | 62 ++ SHADE_Engine/src/Editor/SHEditor.cpp | 22 +- 5 files changed, 693 insertions(+), 8 deletions(-) create mode 100644 SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp create mode 100644 SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 3d673b09..cd596fc6 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -102,6 +102,7 @@ + @@ -188,6 +189,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index e26cacc4..d890c070 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -361,6 +361,7 @@ Tools + @@ -541,5 +542,6 @@ Tools + \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp new file mode 100644 index 00000000..63fd39b8 --- /dev/null +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -0,0 +1,613 @@ + +#include +#include + +#include + +#include "SHImGuiVulkanBackend.h" + +#include + + +#include "Tools/SHLogger.h" +namespace SHADE +{ +#define GETINSTANCE \ + ImGuiIO& io = ImGui::GetIO(); \ + SHBreachInstance& instance = *reinterpret_cast(io.UserData); + + /* + #==============================================================# + || Embedded Shaders || + #==============================================================# + */ + // glsl_shader.vert, compiled with: + // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert + /* + #version 450 core + layout(location = 0) in vec2 aPos; + layout(location = 1) in vec2 aUV; + layout(location = 2) in vec4 aColor; + layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc; + + out gl_PerVertex { vec4 gl_Position; }; + layout(location = 0) out struct { vec4 Color; vec2 UV; } Out; + + void main() + { + Out.Color = aColor; + Out.UV = aUV; + gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); + } + */ + static uint32_t __glsl_shader_vert_spv[] = + { + 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015, + 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43, + 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f, + 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005, + 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000, + 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c, + 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074, + 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001, + 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b, + 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015, + 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047, + 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e, + 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008, + 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002, + 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017, + 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020, + 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015, + 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020, + 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020, + 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020, + 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020, + 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a, + 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014, + 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f, + 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021, + 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006, + 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8, + 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012, + 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016, + 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018, + 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022, + 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008, + 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013, + 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024, + 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006, + 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b, + 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e, + 0x0000002d,0x0000002c,0x000100fd,0x00010038 + }; + + // glsl_shader.frag, compiled with: + // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag + /* + #version 450 core + layout(location = 0) out vec4 fColor; + layout(set=0, binding=0) uniform sampler2D sTexture; + layout(location = 0) in struct { vec4 Color; vec2 UV; } In; + void main() + { + fColor = In.Color * texture(sTexture, In.UV.st); + } + */ + static uint32_t __glsl_shader_frag_spv[] = + { + 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010, + 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000, + 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001, + 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574, + 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e, + 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021, + 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006, + 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003, + 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006, + 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001, + 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020, + 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001, + 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000, + 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000, + 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018, + 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004, + 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d, + 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017, + 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a, + 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085, + 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd, + 0x00010038 + }; + + /* + #==============================================================# + || ImGui Push Constants || + #==============================================================# + */ + struct ImGui_Push_Constants + { + std::array scale; + std::array translate; + }; + + void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice) noexcept + { + if (ImGui::GetCurrentContext() == nullptr) + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + } + else { SHLOG_WARNING("ImGui context already exists") }; + + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. + //io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) + //io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) + io.BackendRendererName = "SHImGuiVulkanBackend"; + + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + ImGuiPlatformIO& platformIO = ImGui::GetPlatformIO(); + platformIO.Renderer_CreateWindow = CreateChildWindow; + platformIO.Renderer_DestroyWindow = DestroyChildWindow; + platformIO.Renderer_SetWindowSize = SetChildWindowSize; + platformIO.Renderer_RenderWindow = RenderChildWindow; + platformIO.Renderer_SwapBuffers = ChildSwapBuffers; + + platformIO.Platform_CreateWindow = CreateChildWindow; + platformIO.Platform_DestroyWindow = DestroyChildWindow; + platformIO.Platform_ShowWindow = [](ImGuiViewport* pViewport) {}; + platformIO.Platform_SetWindowPos = SetChildWindowPos; + platformIO.Platform_GetWindowPos = GetChildWindowPos; + platformIO.Platform_SetWindowSize = SetChildWindowSize; + platformIO.Platform_GetWindowSize = GetChildWindowSize; + //platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus; + //platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus; + //platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized; + platformIO.Platform_SetWindowTitle = [](ImGuiViewport* pViewport, const char*) {}; + platformIO.Platform_RenderWindow = RenderChildWindow; + platformIO.Platform_SwapBuffers = ChildSwapBuffers; + + platformIO.Monitors.resize(0); + ImGuiPlatformMonitor monitor; + monitor.MainPos = monitor.WorkPos = {}; + monitor.MainSize = monitor.WorkSize = {}; + platformIO.Monitors.push_back(monitor); + }//if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + + //associate io keymap with engine input keymap + + //Initialize instance + auto instance = std::make_unique(); + instance->InitializePipeline(io); + instance->lastFrameTime = std::chrono::high_resolution_clock::now(); + io.UserData = instance.release(); + + ImGuiViewport* viewport = ImGui::GetMainViewport(); + viewport->RendererUserData = io.UserData; + viewport->PlatformHandle = io.UserData; + } + + void SHImGuiVulkanBackend::Render(void) noexcept + { + ImGui::Render(); + ImDrawData* drawData = ImGui::GetDrawData(); + //TODO: instance draw data + } + + void SHImGuiVulkanBackend::CreateChildWindow(ImGuiViewport* viewport) noexcept + { + GETINSTANCE + + auto& info = *new SHImGuiWindow{ instance.device }; + viewport->RendererUserData = &info; + } + + void SHImGuiVulkanBackend::DestroyChildWindow(ImGuiViewport* viewport) noexcept + { + GETINSTANCE + + auto info = reinterpret_cast(viewport->RendererUserData); + delete info; + viewport->RendererUserData = nullptr; + } + + ImVec2 SHImGuiVulkanBackend::GetChildWindowSize(ImGuiViewport* viewport) noexcept + { + auto info = reinterpret_cast(viewport->RendererUserData); + return{}; + } + + void SHImGuiVulkanBackend::SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept + { + + } + + ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept + { + } + + void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept + { + } + + void SHImGuiVulkanBackend::RenderChildWindow(ImGuiViewport* viewport, void*) noexcept + { + } + + void SHImGuiVulkanBackend::ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept + { + } + + + void SHImGuiVulkanBackend::SHImGuiWindow::InitializeBuffers(void) noexcept + { + for (auto& primBuffer : primitiveBuffers) + { + primBuffer.vertexBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawVert) * 3000, + nullptr, + sizeof(ImDrawVert) * 3000, + vk::BufferUsageFlagBits::eVertexBuffer, + VMA_MEMORY_USAGE_AUTO, + VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags + + primBuffer.indicesBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawIdx) * 3000, + nullptr, + sizeof(ImDrawIdx) * 3000, + vk::BufferUsageFlagBits::eIndexBuffer, + VMA_MEMORY_USAGE_AUTO, + VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags + + } + } + + + + void SHImGuiVulkanBackend::SHBreachInstance::CreateFontsTexture(Handle image, ImGuiIO& io) noexcept + { + // Build texture atlas + unsigned char* pixels; + int width, height; + + // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. + // If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + + + // Create the texture in xgpu + { + std::array Mip{ height * width * sizeof(std::uint32_t) }; + xgpu::texture::setup Setup; + + Setup.m_Height = height; + Setup.m_Width = width; + Setup.m_MipChain = Mip; + Setup.m_Data = { reinterpret_cast(pixels), Mip[0].m_Size }; + + if (auto Err = m_Device.Create(Texture, Setup); Err) + return Err; + } + + // We could put here the texture id if we wanted + io.Fonts->TexID = nullptr; + + return nullptr; + + } +} +#include +#include + +#include + +#include "SHImGuiVulkanBackend.h" + +#include + + +#include "Tools/SHLogger.h" +namespace SHADE +{ +#define GETINSTANCE \ + ImGuiIO& io = ImGui::GetIO(); \ + SHBreachInstance& instance = *reinterpret_cast(io.UserData); + + /* + #==============================================================# + || Embedded Shaders || + #==============================================================# + */ + // glsl_shader.vert, compiled with: + // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert + /* + #version 450 core + layout(location = 0) in vec2 aPos; + layout(location = 1) in vec2 aUV; + layout(location = 2) in vec4 aColor; + layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc; + + out gl_PerVertex { vec4 gl_Position; }; + layout(location = 0) out struct { vec4 Color; vec2 UV; } Out; + + void main() + { + Out.Color = aColor; + Out.UV = aUV; + gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); + } + */ + static uint32_t __glsl_shader_vert_spv[] = + { + 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015, + 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43, + 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f, + 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005, + 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000, + 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c, + 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074, + 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001, + 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b, + 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015, + 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047, + 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e, + 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008, + 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002, + 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017, + 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020, + 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015, + 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020, + 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020, + 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020, + 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020, + 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a, + 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014, + 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f, + 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021, + 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006, + 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8, + 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012, + 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016, + 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018, + 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022, + 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008, + 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013, + 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024, + 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006, + 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b, + 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e, + 0x0000002d,0x0000002c,0x000100fd,0x00010038 + }; + + // glsl_shader.frag, compiled with: + // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag + /* + #version 450 core + layout(location = 0) out vec4 fColor; + layout(set=0, binding=0) uniform sampler2D sTexture; + layout(location = 0) in struct { vec4 Color; vec2 UV; } In; + void main() + { + fColor = In.Color * texture(sTexture, In.UV.st); + } + */ + static uint32_t __glsl_shader_frag_spv[] = + { + 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010, + 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000, + 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001, + 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574, + 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e, + 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021, + 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006, + 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003, + 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006, + 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001, + 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020, + 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001, + 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000, + 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000, + 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018, + 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004, + 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d, + 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017, + 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a, + 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085, + 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd, + 0x00010038 + }; + + /* + #==============================================================# + || ImGui Push Constants || + #==============================================================# + */ + struct ImGui_Push_Constants + { + std::array scale; + std::array translate; + }; + + void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice) noexcept + { + if (ImGui::GetCurrentContext() == nullptr) + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + } + else { SHLOG_WARNING("ImGui context already exists") }; + + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. + //io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) + //io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) + io.BackendRendererName = "SHImGuiVulkanBackend"; + + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + ImGuiPlatformIO& platformIO = ImGui::GetPlatformIO(); + platformIO.Renderer_CreateWindow = CreateChildWindow; + platformIO.Renderer_DestroyWindow = DestroyChildWindow; + platformIO.Renderer_SetWindowSize = SetChildWindowSize; + platformIO.Renderer_RenderWindow = RenderChildWindow; + platformIO.Renderer_SwapBuffers = ChildSwapBuffers; + + platformIO.Platform_CreateWindow = CreateChildWindow; + platformIO.Platform_DestroyWindow = DestroyChildWindow; + platformIO.Platform_ShowWindow = [](ImGuiViewport* pViewport) {}; + platformIO.Platform_SetWindowPos = SetChildWindowPos; + platformIO.Platform_GetWindowPos = GetChildWindowPos; + platformIO.Platform_SetWindowSize = SetChildWindowSize; + platformIO.Platform_GetWindowSize = GetChildWindowSize; + //platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus; + //platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus; + //platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized; + platformIO.Platform_SetWindowTitle = [](ImGuiViewport* pViewport, const char*) {}; + platformIO.Platform_RenderWindow = RenderChildWindow; + platformIO.Platform_SwapBuffers = ChildSwapBuffers; + + platformIO.Monitors.resize(0); + ImGuiPlatformMonitor monitor; + monitor.MainPos = monitor.WorkPos = {}; + monitor.MainSize = monitor.WorkSize = {}; + platformIO.Monitors.push_back(monitor); + }//if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + + //associate io keymap with engine input keymap + + //Initialize instance + auto instance = std::make_unique(); + instance->InitializePipeline(io); + instance->lastFrameTime = std::chrono::high_resolution_clock::now(); + io.UserData = instance.release(); + + ImGuiViewport* viewport = ImGui::GetMainViewport(); + viewport->RendererUserData = io.UserData; + viewport->PlatformHandle = io.UserData; + } + + void SHImGuiVulkanBackend::Render(void) noexcept + { + ImGui::Render(); + ImDrawData* drawData = ImGui::GetDrawData(); + //TODO: instance draw data + } + + void SHImGuiVulkanBackend::CreateChildWindow(ImGuiViewport* viewport) noexcept + { + GETINSTANCE + + auto& info = *new SHImGuiWindow{ instance.device }; + viewport->RendererUserData = &info; + } + + void SHImGuiVulkanBackend::DestroyChildWindow(ImGuiViewport* viewport) noexcept + { + GETINSTANCE + + auto info = reinterpret_cast(viewport->RendererUserData); + delete info; + viewport->RendererUserData = nullptr; + } + + ImVec2 SHImGuiVulkanBackend::GetChildWindowSize(ImGuiViewport* viewport) noexcept + { + auto info = reinterpret_cast(viewport->RendererUserData); + return{}; + } + + void SHImGuiVulkanBackend::SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept + { + + } + + ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept + { + } + + void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept + { + } + + void SHImGuiVulkanBackend::RenderChildWindow(ImGuiViewport* viewport, void*) noexcept + { + } + + void SHImGuiVulkanBackend::ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept + { + } + + + void SHImGuiVulkanBackend::SHImGuiWindow::InitializeBuffers(void) noexcept + { + for (auto& primBuffer : primitiveBuffers) + { + primBuffer.vertexBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawVert) * 3000, + nullptr, + sizeof(ImDrawVert) * 3000, + vk::BufferUsageFlagBits::eVertexBuffer, + VMA_MEMORY_USAGE_AUTO, + VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags + + primBuffer.indicesBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawIdx) * 3000, + nullptr, + sizeof(ImDrawIdx) * 3000, + vk::BufferUsageFlagBits::eIndexBuffer, + VMA_MEMORY_USAGE_AUTO, + VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags + + } + } + + void SHImGuiVulkanBackend::SHBreachInstance::CreateFontsTexture(Handle image, ImGuiIO& io) noexcept + { + // Build texture atlas + unsigned char* pixels; + int width, height; + + // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. + // If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + + + // Create the texture in xgpu + { + std::array Mip{ height * width * sizeof(std::uint32_t) }; + xgpu::texture::setup Setup; + + Setup.m_Height = height; + Setup.m_Width = width; + Setup.m_MipChain = Mip; + Setup.m_Data = { reinterpret_cast(pixels), Mip[0].m_Size }; + + if (auto Err = m_Device.Create(Texture, Setup); Err) + return Err; + } + + // We could put here the texture id if we wanted + io.Fonts->TexID = nullptr; + + return nullptr; + + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h new file mode 100644 index 00000000..df513df7 --- /dev/null +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include "Graphics/Buffers/SHVkBuffer.h" +#include "Graphics/Images/SHVkImage.h" +#include +#include +#include +#include "Graphics/Swapchain/SHVkSwapchain.h" +#include +#include "Graphics/Debugging/SHVulkanDebugUtil.h" + +namespace SHADE +{ + class SHImGuiVulkanBackend + { + struct SHImGuiWindow + { + struct Buffers + { + Handle vertexBuffer; + Handle indicesBuffer; + + }; + + Handle device; + std::array primitiveBuffers; + + public: + void InitializeBuffers(void) noexcept; + + }; + + struct SHBreachInstance : public SHImGuiWindow + { + Handle pipeline; + std::chrono::time_point lastFrameTime; + public: + void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; + void InitializePipeline(ImGuiIO& io) noexcept; + }; + public: + static void CreateInstance(Handle const& logicalDevice) noexcept; + static void Render(void) noexcept; + + + static void EnableDocking(void) noexcept; + + //Platform specific functions + static void CreateChildWindow(ImGuiViewport* viewport) noexcept; + static void DestroyChildWindow(ImGuiViewport* viewport) noexcept; + static ImVec2 GetChildWindowSize(ImGuiViewport* viewport) noexcept; + static void SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept; + static ImVec2 GetChildWindowPos(ImGuiViewport* viewport) noexcept; + static void SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept; + static void RenderChildWindow(ImGuiViewport* viewport, void*) noexcept; + static void ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept; + + private: + + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index b3bac9d1..ab3a220a 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -3,8 +3,8 @@ #include "SHEditorBackend.h" #include -#include +#include #include #include #include @@ -13,6 +13,8 @@ #include "Tools/SHLogger.h" +extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); + namespace SHADE { Handle SHEditor::cmdPool; @@ -23,14 +25,15 @@ namespace SHADE IMGUI_CHECKVERSION(); ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + SetupWin32Backend(hwnd); SetupVulkanBackend(); - //ImGuiIO& io = ImGui::GetIO(); - - //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; - //io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; } @@ -44,7 +47,7 @@ namespace SHADE NewFrame(); //Add all windows to draw list, Perform necessary updates - ImGui::ShowDemoWindow(); + //ImGui::ShowDemoWindow(); ImGui::Render(); } @@ -59,6 +62,7 @@ namespace SHADE ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); } + } void SHEditor::Exit() @@ -83,6 +87,7 @@ namespace SHADE initInfo.Allocator = nullptr; initInfo.PipelineCache = nullptr; initInfo.Subpass = 0; + initInfo.CheckVkResultFn = [](VkResult err) { if (err == VK_SUCCESS) @@ -105,8 +110,8 @@ namespace SHADE void SHEditor::NewFrame() { - ImGui_ImplWin32_NewFrame(); ImGui_ImplVulkan_NewFrame(); + ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); } @@ -118,4 +123,5 @@ namespace SHADE void SHEditor::ShutdownWin32Backend() { } + } From bf447c1d1d7dcbdef7ec0a807074791cd33ee64b Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 11 Sep 2022 20:03:11 +0800 Subject: [PATCH 16/36] Auto stash before merge of "SP3-4-editor" and "origin/SP3-4-editor" --- SHADE_Engine/src/Graphics/Images/SHVkImage.cpp | 10 +++++++++- SHADE_Engine/src/Graphics/Images/SHVkImage.h | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp index 11fbe5dd..003e36ec 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp @@ -11,6 +11,8 @@ namespace SHADE SHVkImage::SHVkImage( VmaAllocator const& vmaAllocator, SHImageCreateParams const& imageDetails, + unsigned char* data, + uint32_t dataSize, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags ) noexcept @@ -64,7 +66,13 @@ namespace SHADE VmaAllocationInfo allocInfo{}; VkImage tempImage; - vmaCreateImage(vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo&(), &allocCreateInfo, &tempImage, &alloc, &allocInfo); + auto result = vmaCreateImage(vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo&(), &allocCreateInfo, &tempImage, &alloc, &allocInfo); + + if (result != VK_SUCCESS) + SHVulkanDebugUtil::ReportVkError(vk::Result(result), "Failed to create vulkan image. "); + else + SHVulkanDebugUtil::ReportVkSuccess("Successfully created image. "); + vkImage = tempImage; //if (allocFlags & ) diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.h b/SHADE_Engine/src/Graphics/Images/SHVkImage.h index 53066075..48ad1b75 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.h @@ -86,10 +86,11 @@ namespace SHADE /*-----------------------------------------------------------------------*/ SHVkImage(void) noexcept = default; - // TODO: Might need to add flags to parameters SHVkImage( VmaAllocator const& vmaAllocator, SHImageCreateParams const& imageDetails, + unsigned char* data, + uint32_t dataSize, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags ) noexcept; From 47e9e3d3f285d9bd056c42330c45adbfe4d913eb Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 11 Sep 2022 22:34:55 +0800 Subject: [PATCH 17/36] WIP --- .../src/Graphics/Buffers/SHVkBuffer.cpp | 2 + .../src/Graphics/Buffers/SHVkBuffer.h | 1 - .../Graphics/Commands/SHVkCommandBuffer.cpp | 6 + .../src/Graphics/Commands/SHVkCommandBuffer.h | 3 + .../src/Graphics/Images/SHVkImage.cpp | 239 ++++++++++++++---- SHADE_Engine/src/Graphics/Images/SHVkImage.h | 50 +++- 6 files changed, 236 insertions(+), 65 deletions(-) diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp index 4158d3c3..7913574c 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp @@ -33,6 +33,8 @@ namespace SHADE }; cmdBufferHdl->GetVkCommandBuffer().copyBuffer(stagingBuffer, vkBuffer, 1, ©Region); } + + // TODO: Need to destroy staging buffer. Obviously not here but after the command has finished executing. } vk::Buffer SHVkBuffer::GetVkBuffer(void) const noexcept diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h index 2ad3e4e9..9df1a1d0 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h @@ -48,7 +48,6 @@ namespace SHADE vk::BufferUsageFlags bufferUsageFlags; //! Reference to the allocator - //VmaAllocator const& vmaAllocator; std::reference_wrapper vmaAllocator; /*-----------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index 6a307230..a9753416 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -406,6 +406,12 @@ namespace SHADE } + + //void SHVkCommandBuffer::PipelineBarrier(vk::PipelineStageFlags ) const noexcept + //{ + // //vkCommandBuffer.pipelineBarrier() + //} + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index 08fc45f7..4f89f813 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -114,6 +114,9 @@ namespace SHADE void DrawArrays (uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) const noexcept; void DrawIndexed (uint32_t indexCount, uint32_t firstIndex, uint32_t vertexOffset) const noexcept; + // memory barriers + //void PipelineBarrier (void) const noexcept; + // Push Constant variable setting template void SetPushConstantVariable(std::string variableName, T const& data) noexcept diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp index 003e36ec..3cd311d3 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp @@ -5,16 +5,88 @@ #include "Tools/SHLogger.h" #include "SHVkImageView.h" #include "Graphics/Instance/SHVkInstance.h" +#include "Graphics/Buffers/SHVkBuffer.h" namespace SHADE { + /***************************************************************************/ + /*! + + \brief + If an image is a GPU only resource, we need to prep a staging buffer + to use for transferring data to the GPU. #NoteToSelf: I don't really + like this because its duplicate code. Should try to find a way to utilize + the logical device for this. + + \param data + Data to transfer. + + \param srcSize + Size in bytes of the data. + + */ + /***************************************************************************/ + void SHVkImage::PrepStagingBuffer(void* data, uint32_t srcSize) noexcept + { + // For creation of buffer + vk::BufferCreateInfo bufferInfo{}; + + // size stored same as GPU buffer + bufferInfo.size = srcSize; + + // We just want to set the transfer bit + bufferInfo.usage = vk::BufferUsageFlagBits::eTransferSrc; + + // sharing mode exclusive + bufferInfo.sharingMode = vk::SharingMode::eExclusive; + + // Set to auto detect bits + VmaAllocationCreateInfo allocCreateInfo{}; + allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; + + // We want to just write all at once. Using random access bit could make this slow + allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; + + // parameters of a vmaAllocation retrieved via vmaGetAllocationInfo + VmaAllocationInfo allocInfo; + + // results of allocation + VmaAllocation stagingAlloc; + + // To get around VMA's usage for C version of vulkan, create a temp first..., + VkBuffer tempBuffer{}; + + // Create the buffer... + vmaCreateBuffer(*vmaAllocator, + &bufferInfo.operator VkBufferCreateInfo & (), // TODO: Verify if this works (can use renderdoc to check buffer variables?) + &allocCreateInfo, + &tempBuffer, &stagingAlloc, &allocInfo); + + // then assign it to the hpp version + stagingBuffer = tempBuffer; + + // Just map, copy then unmap + void* stagingBufferMappedPtr = nullptr; + vmaMapMemory(*vmaAllocator, stagingAlloc, &stagingBufferMappedPtr); + + if (stagingBufferMappedPtr) + std::memcpy(static_cast(stagingBufferMappedPtr), static_cast(data), srcSize); + + const VkDeviceSize offsets = 0; + const VkDeviceSize sizes = srcSize; + vmaFlushAllocations(*vmaAllocator, 1, &stagingAlloc, &offsets, &sizes); + + vmaUnmapMemory(*vmaAllocator, stagingAlloc); + } + SHVkImage::SHVkImage( - VmaAllocator const& vmaAllocator, - SHImageCreateParams const& imageDetails, - unsigned char* data, - uint32_t dataSize, - VmaMemoryUsage memUsage, - VmaAllocationCreateFlags allocFlags + VmaAllocator const* allocator, + SHImageCreateParams const& imageDetails, + unsigned char* data, + uint32_t dataSize, + std::span inMipOffsets, + VmaMemoryUsage memUsage, + VmaAllocationCreateFlags allocFlags ) noexcept : imageType { imageDetails.imageType } , width{ imageDetails.width } @@ -25,6 +97,11 @@ namespace SHADE , imageFormat{ imageDetails.imageFormat } , usageFlags{} , createFlags{} + , vmaAllocator{allocator} + , mipOffsets { inMipOffsets } + , boundToCoherent{false} + , randomAccessOptimized {false} + , mappedPtr{nullptr} { for (auto& bit : imageDetails.usageBits) usageFlags |= bit; @@ -66,7 +143,7 @@ namespace SHADE VmaAllocationInfo allocInfo{}; VkImage tempImage; - auto result = vmaCreateImage(vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo&(), &allocCreateInfo, &tempImage, &alloc, &allocInfo); + auto result = vmaCreateImage(*vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo&(), &allocCreateInfo, &tempImage, &alloc, &allocInfo); if (result != VK_SUCCESS) SHVulkanDebugUtil::ReportVkError(vk::Result(result), "Failed to create vulkan image. "); @@ -75,55 +152,43 @@ namespace SHADE vkImage = tempImage; - //if (allocFlags & ) + // At this point the image and device memory have been created. + + if (allocFlags & VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT) + randomAccessOptimized = true; + + // TODO: This constructor can only create a GPU only resource for now. Due to time constraint, I was trying to create a ctor + // fast to finish up the ImGUI backend. In the future, there definitely needs to be more versatility to the constructor. + + // Get the memory property flags + VkMemoryPropertyFlags memPropFlags; + vmaGetAllocationMemoryProperties(*vmaAllocator, alloc, &memPropFlags); + + // mainly host visible. Can be cached (need to flush/invalidate), uncached (always coherent) and coherent (virtual). + //if (memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) + //{ + // // If memory is marked to be coherent between CPU and GPU (no need flush/invalidate) (TODO: Verify if VMA_ALLOCATION_CREATE_MAPPED_BIT is used when VMA_MEMORY_USAGE_AUTO is set) + // // TODO: also verify that coherent bit = pointer is already mapped + // if (memPropFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) + // { + // boundToCoherent = true; + // mappedPtr = allocInfo.pMappedData; + // } + // else + // mappedPtr = nullptr; + + // if (data) + // MapWriteUnmap(data, srcSize, 0, 0); + //} + //else + //{ + // We can prep first so that we can do transfers later via 1 cmd buffer recording + PrepStagingBuffer(data, dataSize); + + //} } - /***************************************************************************/ - /*! - - \brief - This is mainly used for images that aren't created internally because - they cannot be created in the traditional way (e.g. swapchain images). - - \param inVkImage - Image already created outside - - \param width - Width of the image - - \param height - Height of the image - - \param depth - Depth of the image - - \param levels - Number of levels in the image - - \param arrayLayers - if the image is an array, this value will be > 1. - - \param imageFormat - Format of the image - - */ - /***************************************************************************/ - SHVkImage::SHVkImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t arrayLayers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept - : vkImage (inVkImage) - , width{ inWidth } - , height{ inHeight } - , depth{ inDepth } - , mipLevelCount{ levels } - , layerCount{ arrayLayers } - , imageFormat{ format } - , usageFlags{flags} - , alloc{} - , imageType{type} - , createFlags{} - { - } - - SHVkImage::SHVkImage(VmaAllocator const& vmaAllocator, uint32_t w, uint32_t h, uint8_t levels, vk::Format format, vk::ImageUsageFlags usage, vk::ImageCreateFlags create) noexcept + SHVkImage::SHVkImage(VmaAllocator const* allocator, uint32_t w, uint32_t h, uint8_t levels, vk::Format format, vk::ImageUsageFlags usage, vk::ImageCreateFlags create) noexcept : width {w} , height{h} , depth {1} @@ -132,6 +197,7 @@ namespace SHADE , imageFormat{format} , usageFlags{usage} , createFlags {create} + , vmaAllocator {allocator} { vk::ImageCreateInfo imageCreateInfo{}; imageCreateInfo.imageType = vk::ImageType::e2D; @@ -157,7 +223,7 @@ namespace SHADE VmaAllocationInfo allocInfo{}; VkImage tempImage; - auto result = vmaCreateImage(vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo & (), &allocCreateInfo, &tempImage, &alloc, &allocInfo); + auto result = vmaCreateImage(*vmaAllocator, &imageCreateInfo.operator VkImageCreateInfo & (), &allocCreateInfo, &tempImage, &alloc, &allocInfo); vkImage = tempImage; if (result != VK_SUCCESS) @@ -171,6 +237,69 @@ namespace SHADE return SHVkInstance::GetResourceManager().Create(inLogicalDeviceHdl, parent, createParams); } + void SHVkImage::TransferToDeviceResource(Handle const& cmdBufferHdl) noexcept + { + // prepare copy regions + std::vector copyRegions{mipOffsets.size()}; + + for (uint32_t i = 0; i < mipOffsets.size(); ++i) + { + copyRegions[i].bufferOffset = mipOffsets[i]; + copyRegions[i].bufferRowLength = 0; // for padding + copyRegions[i].bufferImageHeight = 0; // for padding + copyRegions[i].imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor; // TODO: Need to change this to base it off image format. + copyRegions[i].imageSubresource.mipLevel = i; + copyRegions[i].imageSubresource.baseArrayLayer = 0; // TODO: Array textures not supported yet + copyRegions[i].imageSubresource.layerCount = layerCount; + copyRegions[i].imageOffset = vk::Offset3D{ 0,0,0 }; + copyRegions[i].imageExtent = vk::Extent3D{ width >> i, height >> i, 1 }; + } + + } + + void SHVkImage::TransitionImage(vk::ImageLayout oldLayout, vk::ImageLayout newLayout) noexcept + { + vk::ImageMemoryBarrier barrier{}; + barrier.oldLayout = oldLayout; + barrier.newLayout = newLayout; + barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.image = vkImage; + barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor; // TODO: Need to change this to base it off image format. + barrier.subresourceRange.baseMipLevel = 0; + barrier.subresourceRange.levelCount = mipLevelCount; + barrier.subresourceRange.baseArrayLayer = 0; + barrier.subresourceRange.layerCount = layerCount; + + vk::PipelineStageFlagBits srcStage = vk::PipelineStageFlagBits::eTopOfPipe; + vk::PipelineStageFlagBits dstStage = vk::PipelineStageFlagBits::eTopOfPipe; + + if (oldLayout == vk::ImageLayout::eUndefined && newLayout == vk::ImageLayout::eTransferDstOptimal) + { + srcStage = vk::PipelineStageFlagBits::eTopOfPipe; + dstStage = vk::PipelineStageFlagBits::eTransfer; + + barrier.srcAccessMask = vk::AccessFlagBits::eNone; + barrier.dstAccessMask = vk::AccessFlagBits::eTransferWrite; + } + else if (oldLayout == vk::ImageLayout::eTransferDstOptimal && newLayout == vk::ImageLayout::eShaderReadOnlyOptimal) + { + srcStage = vk::PipelineStageFlagBits::eTransfer; + + // TODO, what if we want to access in compute shader + dstStage = vk::PipelineStageFlagBits::eFragmentShader; + + barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite; + barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead; + } + else + { + SHLOG_ERROR("Image layouts are invalid. "); + } + + //cmdBufferHdl + } + void SHVkImage::LinkWithExteriorImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t layers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept { vkImage = inVkImage; diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.h b/SHADE_Engine/src/Graphics/Images/SHVkImage.h index 48ad1b75..2884cd63 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.h @@ -10,6 +10,7 @@ namespace SHADE { class SHVkLogicalDevice; class SHVkImageView; + class SHVkCommandBuffer; struct SHImageCreateParams { @@ -47,6 +48,12 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ /*-----------------------------------------------------------------------*/ + //! Pointer to the vma allocator. #NoteToSelf: Not super proud of this being a pointer. + //! The only reason why this is a pointer is because a reference_wrapper cannot be default constructed. + //! And the reason why we want a default constructor is because sometimes we don't want to create images + //! but merely link them from outside (swapchain images) + VmaAllocator const* vmaAllocator; + //! 1D, 2D or 3D vk::ImageType imageType = vk::ImageType::e2D; @@ -80,6 +87,29 @@ namespace SHADE //! allocation object containing details of an allocation VmaAllocation alloc{}; + //! Whether or not this image is HOST_VISIBLE and random access optimized + bool randomAccessOptimized; + + //! Whether or not the memory the image is bound to is memory coherent (updates on CPU can be seen on GPU without flushing cache) + bool boundToCoherent; + + //! Persistently mapped pointer if applicable (will be void if image is + //! not created with the correct flags). Note that this is only used for + //! persistent mapping. One time updates do not use this pointer. + void* mappedPtr; + + //! Staging buffer for images purely in the GPU + vk::Buffer stagingBuffer; + + //! Mipmap offsets for initializing the vk::BufferImageCopy during transfer to GPU resource + std::span mipOffsets; + + /*-----------------------------------------------------------------------*/ + /* PRIVATE MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + void PrepStagingBuffer(void* data, uint32_t srcSize) noexcept; + + public: /*-----------------------------------------------------------------------*/ /* CTOR AND DTOR */ @@ -87,16 +117,16 @@ namespace SHADE SHVkImage(void) noexcept = default; SHVkImage( - VmaAllocator const& vmaAllocator, - SHImageCreateParams const& imageDetails, - unsigned char* data, - uint32_t dataSize, - VmaMemoryUsage memUsage, - VmaAllocationCreateFlags allocFlags + VmaAllocator const* allocator, + SHImageCreateParams const& imageDetails, + unsigned char* data, + uint32_t dataSize, + std::span inMipOffsets, + VmaMemoryUsage memUsage, + VmaAllocationCreateFlags allocFlags ) noexcept; - SHVkImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t arrayLayers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept; - SHVkImage(VmaAllocator const& vmaAllocator, uint32_t w, uint32_t h, uint8_t levels, vk::Format format, vk::ImageUsageFlags usage, vk::ImageCreateFlags create) noexcept; + SHVkImage(VmaAllocator const* allocator, uint32_t w, uint32_t h, uint8_t levels, vk::Format format, vk::ImageUsageFlags usage, vk::ImageCreateFlags create) noexcept; SHVkImage(SHVkImage&& rhs) noexcept = default; SHVkImage& operator=(SHVkImage && rhs) noexcept = default; @@ -104,7 +134,9 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - Handle CreateImageView(Handle const& inLogicalDeviceHdl, Handle const& parent, SHImageViewDetails const& createParams) const noexcept; + Handle CreateImageView (Handle const& inLogicalDeviceHdl, Handle const& parent, SHImageViewDetails const& createParams) const noexcept; + void TransferToDeviceResource (Handle const& cmdBufferHdl) noexcept; + void TransitionImage (vk::ImageLayout oldLayout, vk::ImageLayout newLayout) noexcept; /*-----------------------------------------------------------------------*/ /* GETTERS AND SETTERS */ From 506d3a1c3520c6466bb5be46bfd46de4f5ac7a28 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Mon, 12 Sep 2022 20:08:01 +0800 Subject: [PATCH 18/36] WIP --- SHADE_Engine/SHADE_Engine.vcxproj | 2 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 11 +- .../Editor/Backend/SHImGuiVulkanBackend.cpp | 334 +----------------- .../src/Editor/Backend/SHImGuiVulkanBackend.h | 3 + .../Graphics/Commands/SHVkCommandBuffer.cpp | 19 + .../src/Graphics/Commands/SHVkCommandBuffer.h | 9 +- .../Graphics/Devices/SHVkLogicalDevice.cpp | 7 +- .../src/Graphics/Devices/SHVkLogicalDevice.h | 25 +- .../src/Graphics/Images/SHVkImage.cpp | 30 +- SHADE_Engine/src/Graphics/Images/SHVkImage.h | 6 +- 10 files changed, 104 insertions(+), 342 deletions(-) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index cd596fc6..c8dbf732 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -276,4 +276,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index d890c070..1324473d 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -4,6 +4,9 @@ {8C1A20B0-78BC-4A86-6177-5EDA4DB8D1D6} + + {43EDC149-2FBB-B54F-184F-267604BC91B4} + {DBC7D3B0-C769-FE86-B024-12DB9C6585D7} @@ -111,6 +114,9 @@ + + Editor\Backend + Editor @@ -361,9 +367,11 @@ Tools - + + Editor\Backend + Editor @@ -542,6 +550,5 @@ Tools - \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp index 63fd39b8..29a8e703 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -235,6 +235,7 @@ namespace SHADE ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept { + return {}; } void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept @@ -254,14 +255,14 @@ namespace SHADE { for (auto& primBuffer : primitiveBuffers) { - primBuffer.vertexBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawVert) * 3000, + primBuffer.vertexBuffer = device->CreateBuffer(sizeof(ImDrawVert) * 3000, nullptr, sizeof(ImDrawVert) * 3000, vk::BufferUsageFlagBits::eVertexBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags - primBuffer.indicesBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawIdx) * 3000, + primBuffer.indicesBuffer = device->CreateBuffer(sizeof(ImDrawIdx) * 3000, nullptr, sizeof(ImDrawIdx) * 3000, vk::BufferUsageFlagBits::eIndexBuffer, @@ -283,331 +284,28 @@ namespace SHADE // If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + SHImageCreateParams createParams{}; + createParams.imageType = vk::ImageType::e2D; + createParams.width = width; + createParams.height = height; + createParams.depth = 1; + createParams.levels = 1; + createParams.arrayLayers = 1; + createParams.imageFormat = vk::Format::eR8G8B8A8Unorm; + createParams.usageFlags = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst; + createParams.createFlags = {}; + fontsMipOffset.push_back(0); - // Create the texture in xgpu - { - std::array Mip{ height * width * sizeof(std::uint32_t) }; - xgpu::texture::setup Setup; - - Setup.m_Height = height; - Setup.m_Width = width; - Setup.m_MipChain = Mip; - Setup.m_Data = { reinterpret_cast(pixels), Mip[0].m_Size }; - - if (auto Err = m_Device.Create(Texture, Setup); Err) - return Err; - } + fontsTexture = device->CreateImage(createParams, pixels, width * height * sizeof(uint32_t), fontsMipOffset, VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, {}); // We could put here the texture id if we wanted io.Fonts->TexID = nullptr; - - return nullptr; - - } -} -#include -#include - -#include - -#include "SHImGuiVulkanBackend.h" - -#include - - -#include "Tools/SHLogger.h" -namespace SHADE -{ -#define GETINSTANCE \ - ImGuiIO& io = ImGui::GetIO(); \ - SHBreachInstance& instance = *reinterpret_cast(io.UserData); - - /* - #==============================================================# - || Embedded Shaders || - #==============================================================# - */ - // glsl_shader.vert, compiled with: - // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert - /* - #version 450 core - layout(location = 0) in vec2 aPos; - layout(location = 1) in vec2 aUV; - layout(location = 2) in vec4 aColor; - layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc; - - out gl_PerVertex { vec4 gl_Position; }; - layout(location = 0) out struct { vec4 Color; vec2 UV; } Out; - - void main() - { - Out.Color = aColor; - Out.UV = aUV; - gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); - } - */ - static uint32_t __glsl_shader_vert_spv[] = - { - 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b, - 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, - 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015, - 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, - 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43, - 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f, - 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005, - 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000, - 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c, - 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074, - 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001, - 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b, - 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015, - 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047, - 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e, - 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008, - 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002, - 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017, - 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020, - 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015, - 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020, - 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020, - 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020, - 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020, - 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a, - 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014, - 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f, - 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021, - 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006, - 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8, - 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012, - 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016, - 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018, - 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022, - 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008, - 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013, - 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024, - 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006, - 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b, - 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e, - 0x0000002d,0x0000002c,0x000100fd,0x00010038 - }; - - // glsl_shader.frag, compiled with: - // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag - /* - #version 450 core - layout(location = 0) out vec4 fColor; - layout(set=0, binding=0) uniform sampler2D sTexture; - layout(location = 0) in struct { vec4 Color; vec2 UV; } In; - void main() - { - fColor = In.Color * texture(sTexture, In.UV.st); - } - */ - static uint32_t __glsl_shader_frag_spv[] = - { - 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b, - 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, - 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010, - 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, - 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000, - 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001, - 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574, - 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e, - 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021, - 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006, - 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003, - 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006, - 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001, - 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020, - 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001, - 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000, - 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000, - 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018, - 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004, - 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d, - 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017, - 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a, - 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085, - 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd, - 0x00010038 - }; - - /* - #==============================================================# - || ImGui Push Constants || - #==============================================================# - */ - struct ImGui_Push_Constants - { - std::array scale; - std::array translate; - }; - - void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice) noexcept - { - if (ImGui::GetCurrentContext() == nullptr) - { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - } - else { SHLOG_WARNING("ImGui context already exists") }; - - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. - //io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) - //io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) - io.BackendRendererName = "SHImGuiVulkanBackend"; - - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - ImGuiPlatformIO& platformIO = ImGui::GetPlatformIO(); - platformIO.Renderer_CreateWindow = CreateChildWindow; - platformIO.Renderer_DestroyWindow = DestroyChildWindow; - platformIO.Renderer_SetWindowSize = SetChildWindowSize; - platformIO.Renderer_RenderWindow = RenderChildWindow; - platformIO.Renderer_SwapBuffers = ChildSwapBuffers; - - platformIO.Platform_CreateWindow = CreateChildWindow; - platformIO.Platform_DestroyWindow = DestroyChildWindow; - platformIO.Platform_ShowWindow = [](ImGuiViewport* pViewport) {}; - platformIO.Platform_SetWindowPos = SetChildWindowPos; - platformIO.Platform_GetWindowPos = GetChildWindowPos; - platformIO.Platform_SetWindowSize = SetChildWindowSize; - platformIO.Platform_GetWindowSize = GetChildWindowSize; - //platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus; - //platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus; - //platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized; - platformIO.Platform_SetWindowTitle = [](ImGuiViewport* pViewport, const char*) {}; - platformIO.Platform_RenderWindow = RenderChildWindow; - platformIO.Platform_SwapBuffers = ChildSwapBuffers; - - platformIO.Monitors.resize(0); - ImGuiPlatformMonitor monitor; - monitor.MainPos = monitor.WorkPos = {}; - monitor.MainSize = monitor.WorkSize = {}; - platformIO.Monitors.push_back(monitor); - }//if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - - //associate io keymap with engine input keymap - - //Initialize instance - auto instance = std::make_unique(); - instance->InitializePipeline(io); - instance->lastFrameTime = std::chrono::high_resolution_clock::now(); - io.UserData = instance.release(); - - ImGuiViewport* viewport = ImGui::GetMainViewport(); - viewport->RendererUserData = io.UserData; - viewport->PlatformHandle = io.UserData; } - void SHImGuiVulkanBackend::Render(void) noexcept - { - ImGui::Render(); - ImDrawData* drawData = ImGui::GetDrawData(); - //TODO: instance draw data - } - - void SHImGuiVulkanBackend::CreateChildWindow(ImGuiViewport* viewport) noexcept - { - GETINSTANCE - - auto& info = *new SHImGuiWindow{ instance.device }; - viewport->RendererUserData = &info; - } - - void SHImGuiVulkanBackend::DestroyChildWindow(ImGuiViewport* viewport) noexcept - { - GETINSTANCE - - auto info = reinterpret_cast(viewport->RendererUserData); - delete info; - viewport->RendererUserData = nullptr; - } - - ImVec2 SHImGuiVulkanBackend::GetChildWindowSize(ImGuiViewport* viewport) noexcept - { - auto info = reinterpret_cast(viewport->RendererUserData); - return{}; - } - - void SHImGuiVulkanBackend::SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept + void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io) noexcept { } - ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept - { - } - - void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept - { - } - - void SHImGuiVulkanBackend::RenderChildWindow(ImGuiViewport* viewport, void*) noexcept - { - } - - void SHImGuiVulkanBackend::ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept - { - } - - - void SHImGuiVulkanBackend::SHImGuiWindow::InitializeBuffers(void) noexcept - { - for (auto& primBuffer : primitiveBuffers) - { - primBuffer.vertexBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawVert) * 3000, - nullptr, - sizeof(ImDrawVert) * 3000, - vk::BufferUsageFlagBits::eVertexBuffer, - VMA_MEMORY_USAGE_AUTO, - VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags - - primBuffer.indicesBuffer = logicalDeviceHdl->CreateBuffer(sizeof(ImDrawIdx) * 3000, - nullptr, - sizeof(ImDrawIdx) * 3000, - vk::BufferUsageFlagBits::eIndexBuffer, - VMA_MEMORY_USAGE_AUTO, - VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags - - } - } - - void SHImGuiVulkanBackend::SHBreachInstance::CreateFontsTexture(Handle image, ImGuiIO& io) noexcept - { - // Build texture atlas - unsigned char* pixels; - int width, height; - - // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. - // If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. - io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); - - - - // Create the texture in xgpu - { - std::array Mip{ height * width * sizeof(std::uint32_t) }; - xgpu::texture::setup Setup; - - Setup.m_Height = height; - Setup.m_Width = width; - Setup.m_MipChain = Mip; - Setup.m_Data = { reinterpret_cast(pixels), Mip[0].m_Size }; - - if (auto Err = m_Device.Create(Texture, Setup); Err) - return Err; - } - - // We could put here the texture id if we wanted - io.Fonts->TexID = nullptr; - - return nullptr; - - } } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h index df513df7..fdfa4160 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "Graphics/Buffers/SHVkBuffer.h" #include "Graphics/Images/SHVkImage.h" @@ -35,6 +36,8 @@ namespace SHADE { Handle pipeline; std::chrono::time_point lastFrameTime; + Handle fontsTexture; + std::vector fontsMipOffset; public: void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; void InitializePipeline(ImGuiIO& io) noexcept; diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index a9753416..0cc9138e 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -407,6 +407,25 @@ namespace SHADE } + void SHVkCommandBuffer::PipelineBarrier ( + vk::PipelineStageFlags srcStage, + vk::PipelineStageFlags dstStage, + vk::DependencyFlags deps, + std::vector const& memoryBarriers, + std::vector const& bufferMemoryBarriers, + std::vector const& imageMemoryBarriers + ) const noexcept + { + vkCommandBuffer.pipelineBarrier ( + srcStage, + dstStage, + deps, + memoryBarriers, + bufferMemoryBarriers, + imageMemoryBarriers + ); + } + //void SHVkCommandBuffer::PipelineBarrier(vk::PipelineStageFlags ) const noexcept //{ // //vkCommandBuffer.pipelineBarrier() diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index 4f89f813..932a0d47 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -115,7 +115,14 @@ namespace SHADE void DrawIndexed (uint32_t indexCount, uint32_t firstIndex, uint32_t vertexOffset) const noexcept; // memory barriers - //void PipelineBarrier (void) const noexcept; + void PipelineBarrier ( + vk::PipelineStageFlags srcStage, + vk::PipelineStageFlags dstStage, + vk::DependencyFlags deps, + std::vector const& memoryBarriers, + std::vector const& bufferMemoryBarriers, + std::vector const& imageMemoryBarriers + ) const noexcept; // Push Constant variable setting template diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index 0fa1c864..69e76246 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -418,7 +418,12 @@ namespace SHADE /***************************************************************************/ Handle SHVkLogicalDevice::CreateImage(uint32_t w, uint32_t h, uint8_t levels, vk::Format format, vk::ImageUsageFlags usage, vk::ImageCreateFlags create) const noexcept { - return SHVkInstance::GetResourceManager().Create(std::cref(vmaAllocator), w, h, levels, format, usage, create); + return SHVkInstance::GetResourceManager().Create(&vmaAllocator, w, h, levels, format, usage, create); + } + + Handle SHVkLogicalDevice::CreateImage(SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, std::span inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags) noexcept + { + return SHVkInstance::GetResourceManager().Create(&vmaAllocator, imageDetails, data, dataSize, inMipOffsets, memUsage, allocFlags); } /***************************************************************************/ diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index 78108b94..d50d27f0 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -19,6 +19,7 @@ #include "vk_mem_alloc.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" +#include "Graphics/Images/SHVkImage.h" namespace SHADE { @@ -29,7 +30,6 @@ namespace SHADE class SHVkSurface; class SHVkSwapchain; class SHVkBuffer; - class SHVkImage; class SHVkFence; class SHVkSemaphore; class SHVkShaderModule; @@ -138,14 +138,23 @@ namespace SHADE ) const noexcept; Handle CreateImage ( - uint32_t w, - uint32_t h, - uint8_t levels, - vk::Format format, - vk::ImageUsageFlags usage, - vk::ImageCreateFlags create + uint32_t w, + uint32_t h, + uint8_t levels, + vk::Format format, + vk::ImageUsageFlags usage, + vk::ImageCreateFlags create ) const noexcept; - + + Handle CreateImage ( + SHImageCreateParams const& imageDetails, + unsigned char* data, + uint32_t dataSize, + std::span inMipOffsets, + VmaMemoryUsage memUsage, + VmaAllocationCreateFlags allocFlags + ) noexcept; + Handle CreateShaderModule ( std::vector const& binaryData, std::string entryPoint, diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp index 3cd311d3..f3d5a7b5 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp @@ -103,11 +103,8 @@ namespace SHADE , randomAccessOptimized {false} , mappedPtr{nullptr} { - for (auto& bit : imageDetails.usageBits) - usageFlags |= bit; - - for (auto& bit : imageDetails.createBits) - createFlags |= bit; + usageFlags = imageDetails.usageFlags; + createFlags = imageDetails.createFlags; // If marked as 2D array compatible, image type MUST be 3D if (createFlags & vk::ImageCreateFlagBits::e2DArrayCompatible) @@ -257,9 +254,27 @@ namespace SHADE } - void SHVkImage::TransitionImage(vk::ImageLayout oldLayout, vk::ImageLayout newLayout) noexcept + /***************************************************************************/ + /*! + + \brief + Does not perform any image transitions but prepares a barrier for image + transitioning. Pipeline barrier will be issued outside this call after + this preparation function, or at least, it should be. + + \param oldLayout + Old layout of the image. + + \param newLayout + new layout of the image to transition to. + + \param barrier + Barrier to modify to prepare the image for transitioning. + + */ + /***************************************************************************/ + void SHVkImage::PrepareImageTransition(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept { - vk::ImageMemoryBarrier barrier{}; barrier.oldLayout = oldLayout; barrier.newLayout = newLayout; barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; @@ -297,7 +312,6 @@ namespace SHADE SHLOG_ERROR("Image layouts are invalid. "); } - //cmdBufferHdl } void SHVkImage::LinkWithExteriorImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t layers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.h b/SHADE_Engine/src/Graphics/Images/SHVkImage.h index 2884cd63..a30b90e6 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.h @@ -36,10 +36,10 @@ namespace SHADE vk::Format imageFormat; //! Image usage bits - std::span usageBits; + vk::ImageUsageFlags usageFlags; //! Image create flags - std::span createBits; + vk::ImageCreateFlags createFlags; }; class SHVkImage @@ -136,7 +136,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ Handle CreateImageView (Handle const& inLogicalDeviceHdl, Handle const& parent, SHImageViewDetails const& createParams) const noexcept; void TransferToDeviceResource (Handle const& cmdBufferHdl) noexcept; - void TransitionImage (vk::ImageLayout oldLayout, vk::ImageLayout newLayout) noexcept; + void PrepareImageTransition (vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept; /*-----------------------------------------------------------------------*/ /* GETTERS AND SETTERS */ From 8f7d8cd8bc29726570b4332fdcafc7e54fc2dd57 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Tue, 13 Sep 2022 09:13:39 +0800 Subject: [PATCH 19/36] Added vertex attributes --- SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp | 9 +++++++++ SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h | 2 ++ SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.cpp | 5 ++++- .../src/Graphics/VertexDescriptors/SHVertexAttribute.h | 5 ++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp index 29a8e703..f00a2072 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -305,7 +305,16 @@ namespace SHADE void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io) noexcept { + SHVertexInputState vInputState{}; + vInputState.AddBinding(false, true, + { + SHVertexAttribute(SHAttribFormat::FLOAT_2D), + SHVertexAttribute(SHAttribFormat::FLOAT_2D), + SHVertexAttribute(SHAttribFormat::UINT32_1D), + } + ); + //Handle newPipeline = device->CreatePipeline() } } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h index fdfa4160..387d173d 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -35,11 +35,13 @@ namespace SHADE struct SHBreachInstance : public SHImGuiWindow { Handle pipeline; + Handle renderpass; std::chrono::time_point lastFrameTime; Handle fontsTexture; std::vector fontsMipOffset; public: void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; + void InitializeRenderpass (void) noexcept; void InitializePipeline(ImGuiIO& io) noexcept; }; public: diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.cpp b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.cpp index 8179444f..6094958e 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.cpp +++ b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.cpp @@ -117,7 +117,7 @@ namespace SHADE dirty = isDirty; } - std::tuple SHVertexInputState::GetInfoFromAttribFormat(SHAttribFormat attribFormat) const noexcept + std::tuple SHVertexInputState::GetInfoFromAttribFormat(SHAttribFormat attribFormat) const noexcept { switch (attribFormat) { @@ -138,6 +138,9 @@ namespace SHADE case SHAttribFormat::MAT_4D: return std::make_tuple(4, 16, vk::Format::eR32G32B32A32Sfloat); break; + + case SHAttribFormat::UINT32_1D: + return std::make_tuple(1, 4, vk::Format::eR32Uint); } return std::make_tuple(0, 0, vk::Format::eR32Sfloat); } diff --git a/SHADE_Engine/src/Graphics/VertexDescriptors/SHVertexAttribute.h b/SHADE_Engine/src/Graphics/VertexDescriptors/SHVertexAttribute.h index fd8ee3d1..b216f5f4 100644 --- a/SHADE_Engine/src/Graphics/VertexDescriptors/SHVertexAttribute.h +++ b/SHADE_Engine/src/Graphics/VertexDescriptors/SHVertexAttribute.h @@ -18,7 +18,10 @@ namespace SHADE // that a mat2 can be interpreted as (x, y, x, y), (o, o, o, o) instead of (x, y, o, o), (o, o, o, o)? MAT_2D, MAT_3D, - MAT_4D + MAT_4D, + + // integer formats + UINT32_1D, }; struct SHVertexAttribute From 8b8f0a076d6ae349143dcbbb22949871d0f3fd4f Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 13:57:01 +0800 Subject: [PATCH 20/36] Added const to some functions --- SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp | 12 ++++++---- SHADE_Engine/src/ECS_Base/Entity/SHEntity.h | 22 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp index 6005fb01..08139158 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp @@ -28,7 +28,7 @@ namespace SHADE //SHEntityManager::RemoveEntity(this->entityID); } - EntityID SHEntity::GetEID() noexcept + EntityID SHEntity::GetEID()const noexcept { return this->entityID; } @@ -39,6 +39,10 @@ namespace SHADE SHComponentManager::SetActive(entityID, active); } + bool SHEntity::GetActive(void) const noexcept + { + return isActive; + } void SHEntity::SetParent(SHEntity* newParent) noexcept @@ -53,20 +57,20 @@ namespace SHADE //TODO } - SHEntity* SHEntity::GetParent() noexcept + SHEntity* SHEntity::GetParent()const noexcept { //TODO return nullptr; } - std::vectorconst& SHEntity::GetChildren() noexcept + std::vectorconst& SHEntity::GetChildren()const noexcept { //TODO return std::vector{}; } - std::vectorconst& SHEntity::GetChildrenID() noexcept + std::vectorconst& SHEntity::GetChildrenID()const noexcept { return std::vector{}; } diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h index d499042c..0e00c8af 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h @@ -63,7 +63,7 @@ namespace SHADE * Returns nullptr if the entity does not have such Component. ***************************************************************************/ template - std::enable_if_t, T*> GetComponent() noexcept + std::enable_if_t, T*> GetComponent()const noexcept { return SHComponentManager::GetComponent_s(entityID); @@ -77,7 +77,7 @@ namespace SHADE * \return uint32_t * The entityID of this Entity object. ***************************************************************************/ - EntityID GetEID() noexcept; + EntityID GetEID()const noexcept; /*!************************************************************************* * \brief Set the Active object @@ -91,6 +91,7 @@ namespace SHADE ***************************************************************************/ virtual void SetActive(bool active) noexcept; + bool GetActive(void)const noexcept; /************************************************************************** @@ -124,7 +125,7 @@ namespace SHADE * Returns a pointer to the parent entity. * Returns a nullptr if the parent node is the root node. ***************************************************************************/ - SHEntity* GetParent() noexcept; + SHEntity* GetParent()const noexcept; /************************************************************************** @@ -133,7 +134,7 @@ namespace SHADE * \return * Return a vector of SHEntity pointers of the children belonging to this entity. ***************************************************************************/ - std::vectorconst& GetChildren() noexcept; + std::vectorconst& GetChildren()const noexcept; /************************************************************************** * \brief @@ -141,11 +142,13 @@ namespace SHADE * \return * return a vector of EntityID of the children belonging to this entity. ***************************************************************************/ - std::vectorconst& GetChildrenID() noexcept; - + std::vectorconst& GetChildrenID()const noexcept; + //Name of the entity. This name is non-unique and only used for the editor. std::string name; - bool isActive; + + + private: @@ -156,6 +159,11 @@ namespace SHADE EntityID entityID; + //Entity active state. This should only be set using the SetActive function which will + //set the active state of all components of this entity. + bool isActive; + + }; } From 51fc65b825fc184ef05a46ef2b936b837548c3de Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 14:07:40 +0800 Subject: [PATCH 21/36] Added some typedefs --- SHADE_Engine/src/ECS_Base/SHECSMacros.h | 2 ++ .../src/ECS_Base/System/SHComponentManager.h | 22 ++++++++++++------- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 5 +++++ .../src/ECS_Base/System/SHSystemManager.h | 7 +++++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/SHADE_Engine/src/ECS_Base/SHECSMacros.h b/SHADE_Engine/src/ECS_Base/SHECSMacros.h index 5243e20f..0a8971e7 100644 --- a/SHADE_Engine/src/ECS_Base/SHECSMacros.h +++ b/SHADE_Engine/src/ECS_Base/SHECSMacros.h @@ -11,6 +11,8 @@ typedef uint16_t EntityIndex; typedef uint32_t ComponentTypeID; + + const EntityIndex MAX_EID = 51000; diff --git a/SHADE_Engine/src/ECS_Base/System/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/System/SHComponentManager.h index 2f6ff504..2c747663 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/System/SHComponentManager.h @@ -25,6 +25,10 @@ namespace SHADE { + + + typedef SHFamilyID ComponentFamily; + class SHComponentManager { private: @@ -36,6 +40,8 @@ namespace SHADE friend struct SHSceneNode; + + /************************************************************************** @@ -225,7 +231,7 @@ namespace SHADE * \return * none ***************************************************************************/ - static void AddComponent(EntityID entityID, uint32_t componentTypeID) noexcept + static void AddComponent(EntityID entityID, ComponentTypeID componentTypeID) noexcept { componentSet.GetSparseSet_ID(componentTypeID)->Add(EntityHandleGenerator::GetIndex(entityID)); @@ -277,7 +283,7 @@ namespace SHADE * \return bool * True if the entity has a component of specified type. ***************************************************************************/ - static bool HasComponent_ID(EntityID entityID, uint32_t componentTypeID) noexcept; + static bool HasComponent_ID(EntityID entityID, ComponentTypeID componentTypeID) noexcept; /*!************************************************************************* @@ -362,7 +368,7 @@ namespace SHADE * \return * ***************************************************************************/ - static void SwapInDenseByIndexHash_ID(EntityIndex index, EntityID hash, uint32_t componentTypeID) noexcept; + static void SwapInDenseByIndexHash_ID(EntityIndex index, EntityID hash, ComponentTypeID componentTypeID) noexcept; @@ -385,7 +391,7 @@ namespace SHADE * \return * The number of components in the component sparse set. ***************************************************************************/ - static EntityIndex ComponentCount_ID(uint32_t componentTypeID) + static EntityIndex ComponentCount_ID(ComponentTypeID componentTypeID) { return componentSet.GetSparseSet_ID(componentTypeID)->Count(); } @@ -402,9 +408,9 @@ namespace SHADE static void SetActive(EntityID entityID, bool active) noexcept; template - static std::enable_if_t<(... && std::is_base_of_v), uint32_t> CreateComponentGroup(uint32_t numOwningComponents) + static std::enable_if_t<(... && std::is_base_of_v), uint32_t> CreateComponentGroup(ComponentTypeID numOwningComponents) { - std::vector templateIDs{ (SHFamilyID::GetID())... }; + std::vector templateIDs{ (ComponentFamily::GetID())... }; for (auto& g : componentGroups) { @@ -421,11 +427,11 @@ namespace SHADE } SHComponentGroup grp; - for (uint32_t i = 0; i < numOwningComponents; ++i) + for (ComponentTypeID i = 0; i < numOwningComponents; ++i) { grp.ownedComponentTypes.push_back(templateIDs[i]); } - for (uint32_t i = 0; i < templateIDs.size(); ++i) + for (ComponentTypeID i = 0; i < templateIDs.size(); ++i) { grp.componentTypeIDs.push_back(templateIDs[i]); } diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 1a304605..817b9718 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -11,8 +11,13 @@ #ifndef SH_SYSTEM_H #define SH_SYSTEM_H + + namespace SHADE { + + + class SHSystem { protected: diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h index d5a4866d..a81370e9 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h @@ -20,11 +20,16 @@ #include #include #include "../System/SHSystem.h" - +#include "../General/SHFamily.h" namespace SHADE { + typedef uint32_t SystemTypeID; + typedef uint32_t SystemVersionID; + typedef uint64_t SystemID; + typedef SHFamilyID SystemFamily; + class SHSystemManager { //type definition for the container we use to store our system From d483916cc59c0cf17ed48677d8d804be74b267c3 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 14:28:32 +0800 Subject: [PATCH 22/36] Changed SystemManager to use SystemID map instead of using a string map --- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 1 + .../src/ECS_Base/System/SHSystemManager.cpp | 11 ------ .../src/ECS_Base/System/SHSystemManager.h | 35 ++++++++++++++----- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 817b9718..dd84fe64 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -55,6 +55,7 @@ namespace SHADE virtual void Exit() = 0; }; + } #endif \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp index 67d6f781..5b861161 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp @@ -19,17 +19,6 @@ namespace SHADE SHSystemManager::SystemContainer SHSystemManager::systemContainer; - SHSystem* SHSystemManager::GetSystem(std::string name) - { - if (systemContainer.find(name) == systemContainer.end()) - { - assert("Get System Error: No system with such name exist."); - return nullptr; - } - - return systemContainer.find(name)->second.get(); - } - void SHSystemManager::Init() noexcept { for (auto& system : systemContainer) diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h index a81370e9..dada878b 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "../System/SHSystem.h" #include "../General/SHFamily.h" @@ -33,7 +34,7 @@ namespace SHADE class SHSystemManager { //type definition for the container we use to store our system - using SystemContainer = std::unordered_map>; + using SystemContainer = std::unordered_map>; private: static SystemContainer systemContainer; @@ -57,14 +58,18 @@ namespace SHADE * none ***************************************************************************/ template - static std::enable_if_t, void> CreateSystem(std::string const& name) + static std::enable_if_t, void> CreateSystem() { - if (systemContainer.find(name) != systemContainer.end()) - { - assert("System Creation Error: System with the same name already exist."); - } + SystemTypeID typeID = SystemFamily::GetID(); - systemContainer.emplace(name, std::make_unique()); + SystemVersionID version = 0; + SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; + while (systemContainer.find(id) != systemContainer.end()) + { + ++version; + SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; + } + systemContainer.emplace(id, std::make_unique()); } @@ -77,7 +82,21 @@ namespace SHADE * \return * Base System pointer. ***************************************************************************/ - static SHSystem* GetSystem(std::string name); + template + static std::enable_if_t, T*> GetSystem(SystemVersionID version = 0) + { + SystemTypeID typeID = SystemFamily::GetID(); + + SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; + + if (systemContainer.find(id) == systemContainer.end()) + { + std::cout << "System Manager error: System Version " << version << " does not exit." << std::endl; + return nullptr; + } + + return (T*)systemContainer.find(id)->second.get(); + } /************************************************************************** * \brief From e9241feb69608b5cd4f6ceedc958473d0dea6714 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 15:26:23 +0800 Subject: [PATCH 23/36] moved all managers to their own folder --- .../SHComponentManager.cpp | 0 .../{System => Managers}/SHComponentManager.h | 0 .../{System => Managers}/SHEntityManager.cpp | 0 .../{System => Managers}/SHEntityManager.h | 0 .../{System => Managers}/SHSystemManager.cpp | 0 .../{System => Managers}/SHSystemManager.h | 0 SHADE_Engine/src/ECS_Base/System/SHSystem.h | 36 ++++++++++++++++++- 7 files changed, 35 insertions(+), 1 deletion(-) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHComponentManager.cpp (100%) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHComponentManager.h (100%) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHEntityManager.cpp (100%) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHEntityManager.h (100%) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHSystemManager.cpp (100%) rename SHADE_Engine/src/ECS_Base/{System => Managers}/SHSystemManager.h (100%) diff --git a/SHADE_Engine/src/ECS_Base/System/SHComponentManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.cpp similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHComponentManager.cpp rename to SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.cpp diff --git a/SHADE_Engine/src/ECS_Base/System/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHComponentManager.h rename to SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h diff --git a/SHADE_Engine/src/ECS_Base/System/SHEntityManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHEntityManager.cpp rename to SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp diff --git a/SHADE_Engine/src/ECS_Base/System/SHEntityManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHEntityManager.h rename to SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHSystemManager.cpp rename to SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h similarity index 100% rename from SHADE_Engine/src/ECS_Base/System/SHSystemManager.h rename to SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index dd84fe64..dd25db69 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -16,7 +16,7 @@ namespace SHADE { - + class SHSystemManager; class SHSystem { @@ -54,8 +54,42 @@ namespace SHADE ***************************************************************************/ virtual void Exit() = 0; + + protected: + SystemID systemID{}; + }; + + + + + class SHTestSystem : public SHSystem + { + public: + + SHTestSystem() = default; + ~SHTestSystem() = default; + + int value{5}; + + void Init() + { + + } + + void Run(float dt) + { + + } + + void Exit() + { + + } + }; + + } #endif \ No newline at end of file From 8842269c6d3baa4a764e2a9e86bc7a0ae1520616 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 15:36:03 +0800 Subject: [PATCH 24/36] Changed everything to use pragma once --- SHADE_Engine/SHADE_Engine.vcxproj | 12 +++---- SHADE_Engine/SHADE_Engine.vcxproj.filters | 33 ++++++++++--------- .../src/ECS_Base/Components/SHComponent.h | 3 +- .../ECS_Base/Components/SHComponentGroup.cpp | 2 +- .../ECS_Base/Components/SHComponentGroup.h | 7 +--- SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp | 4 +-- SHADE_Engine/src/ECS_Base/Entity/SHEntity.h | 8 ++--- .../src/ECS_Base/General/SHSparseBase.h | 3 +- .../src/ECS_Base/General/SHSparseSet.h | 3 +- .../ECS_Base/General/SHSparseSetContainer.h | 3 +- .../ECS_Base/Managers/SHComponentManager.h | 8 ++--- .../src/ECS_Base/Managers/SHEntityManager.h | 8 +---- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 2 +- .../src/ECS_Base/System/SHSystemRoutine.h | 1 + SHADE_Engine/src/Scene/SHSceneGraph.cpp | 2 +- SHADE_Engine/src/Scene/SHSceneManager.cpp | 6 ++-- 16 files changed, 43 insertions(+), 62 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index e54c82ec..663c60aa 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -110,11 +110,11 @@ + + + - - - @@ -196,9 +196,9 @@ - - - + + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 7486fad4..19ff17bf 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -13,6 +13,9 @@ {07FEB307-F3F6-D259-1C29-B8DE0881B265} + + {37718753-A387-85E9-ECFE-100B58545671} + {EE037863-5A8F-E527-63A0-681CCFAA4128} @@ -141,21 +144,21 @@ ECS_Base\General + + ECS_Base\Managers + + + ECS_Base\Managers + + + ECS_Base\Managers + ECS_Base - - ECS_Base\System - - - ECS_Base\System - ECS_Base\System - - ECS_Base\System - Engine @@ -393,14 +396,14 @@ ECS_Base\Entity - - ECS_Base\System + + ECS_Base\Managers - - ECS_Base\System + + ECS_Base\Managers - - ECS_Base\System + + ECS_Base\Managers Engine diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h index c9c5e6f1..20335eab 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h @@ -9,8 +9,7 @@ *********************************************************************/ -#ifndef SH_COMPONENT_H -#define SH_COMPONENT_H +#pragma once #include "SHpch.h" #include "../SHECSMacros.h" diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.cpp b/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.cpp index 3ccb7778..2627d9fb 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.cpp +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.cpp @@ -14,7 +14,7 @@ #include "SHpch.h" #include "SHComponentGroup.h" -#include "../System/SHComponentManager.h" +#include "../Managers/SHComponentManager.h" namespace SHADE diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.h b/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.h index 3f67aff0..2b20ec13 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.h +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponentGroup.h @@ -12,9 +12,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ - -#ifndef SH_COMPONENT_GROUP -#define SH_COMPONENT_GROUP +#pragma once #include "../SHECSMacros.h" #include "../General/SHFamily.h" @@ -180,6 +178,3 @@ namespace SHADE }; } - - -#endif diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp index 08139158..ef613390 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp @@ -9,9 +9,9 @@ *********************************************************************/ #include "SHpch.h" #include "SHEntity.h" -#include "../System/SHEntityManager.h" +#include "../Managers/SHEntityManager.h" //#include "Scene/SHSceneGraph.h" -#include "../System/SHComponentManager.h" +#include "../Managers/SHComponentManager.h" namespace SHADE { diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h index 0e00c8af..ab73f45f 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h @@ -8,12 +8,11 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_ENTITY_H -#define SH_ENTITY_H +#pragma once #include "../SHECSMacros.h" #include "../Components/SHComponent.h" -#include "../System/SHComponentManager.h" +#include "../Managers/SHComponentManager.h" //#include "../../Scene/SHSceneNode.h" @@ -166,6 +165,3 @@ namespace SHADE }; } - - -#endif diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h b/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h index 2deebe74..f83d3052 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h @@ -10,8 +10,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_SPARSE_BASE_H -#define SH_SPARSE_BASE_H +#pragma once #include "../SHECSMacros.h" diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h b/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h index 3473eb37..6dada871 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h @@ -9,8 +9,7 @@ or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_SPARSE_SET_H -#define SH_SPARSE_SET_H +#pragma once #include "../SHECSMacros.h" #include "../General/SHSparseBase.h" diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h b/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h index 8698d1f5..dbba0021 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h @@ -10,8 +10,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_SPARSE_SET_CONTAINER_H -#define SH_SPARSE_SET_CONTAINER_H +#pragma once #include "SHSparseSet.h" #include "SHFamily.h" diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h index 2c747663..995e8b24 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h @@ -12,8 +12,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_ENGINE_H -#define SH_ENGINE_H +#pragma once #include "../General/SHSparseSetContainer.h" #include "../Components/SHComponent.h" @@ -477,7 +476,4 @@ namespace SHADE -} - - -#endif +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h index 7afece52..bec60a17 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h @@ -12,9 +12,7 @@ or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ - -#ifndef SH_ENTITY_MANAGER_H -#define SH_ENTITY_MANAGER_H +#pragma once #include #include @@ -198,7 +196,3 @@ namespace SHADE } - - - -#endif diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index dd25db69..0d48c721 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -56,7 +56,7 @@ namespace SHADE protected: - SystemID systemID{}; + }; diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h new file mode 100644 index 00000000..6f70f09b --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h @@ -0,0 +1 @@ +#pragma once diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.cpp b/SHADE_Engine/src/Scene/SHSceneGraph.cpp index 9c99fb5a..9fddf10e 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.cpp +++ b/SHADE_Engine/src/Scene/SHSceneGraph.cpp @@ -14,7 +14,7 @@ #include "SHSceneGraph.h" // Project Headers -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" #include "Tools/SHLogger.h" #include "Tools/SHException.h" diff --git a/SHADE_Engine/src/Scene/SHSceneManager.cpp b/SHADE_Engine/src/Scene/SHSceneManager.cpp index a60df922..d5223af8 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.cpp +++ b/SHADE_Engine/src/Scene/SHSceneManager.cpp @@ -10,11 +10,11 @@ *********************************************************************/ #include "SHpch.h" #include "SHSceneManager.h" -#include "ECS_Base/System/SHComponentManager.h" +#include "ECS_Base/Managers/SHComponentManager.h" //#include "Input/SHInputManager.h" //#include "Rendering/Window/SHRenderingWindow.h" -#include "ECS_Base/System/SHEntityManager.h" -#include "ECS_Base/System/SHSystemManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" +#include "ECS_Base/Managers/SHSystemManager.h" //#include "FRC/SHFrameRateController.h" //#include "ECS_Base/System/SHApplication.h" From f342fdcb950ef3276b73353ae5bd22954ed04893 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 13 Sep 2022 15:36:34 +0800 Subject: [PATCH 25/36] changed more stuff to pragma once --- SHADE_Engine/SHADE_Engine.vcxproj | 1 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 1 + SHADE_Engine/src/ECS_Base/Components/SHComponent.h | 1 - SHADE_Engine/src/ECS_Base/General/SHFamily.h | 9 ++------- SHADE_Engine/src/ECS_Base/General/SHHandleGenerator.h | 8 ++------ SHADE_Engine/src/ECS_Base/General/SHSparseBase.h | 5 +---- SHADE_Engine/src/ECS_Base/General/SHSparseSet.h | 4 +--- .../src/ECS_Base/General/SHSparseSetContainer.h | 3 --- SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h | 10 ++-------- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 9 ++------- SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h | 3 +++ 11 files changed, 15 insertions(+), 39 deletions(-) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 663c60aa..1831bff6 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -115,6 +115,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 19ff17bf..70a5b407 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -385,6 +385,7 @@ Tools + diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h index 20335eab..66d1d8c4 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h @@ -117,4 +117,3 @@ namespace SHADE }; } -#endif diff --git a/SHADE_Engine/src/ECS_Base/General/SHFamily.h b/SHADE_Engine/src/ECS_Base/General/SHFamily.h index f97bbe0e..5815703f 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHFamily.h +++ b/SHADE_Engine/src/ECS_Base/General/SHFamily.h @@ -11,8 +11,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_FAMILY_H -#define SH_FAMILY_H +#pragma once #include "../SHECSMacros.h" @@ -81,8 +80,4 @@ namespace SHADE template ComponentTypeID SHFamilyID::currentID = 0; -} - - - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/General/SHHandleGenerator.h b/SHADE_Engine/src/ECS_Base/General/SHHandleGenerator.h index 48ae2132..7c68f66d 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHHandleGenerator.h +++ b/SHADE_Engine/src/ECS_Base/General/SHHandleGenerator.h @@ -11,8 +11,7 @@ or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_HANDLE_GENERATOR_H -#define SH_HANDLE_GENERATOR_H +#pragma once #include #include @@ -299,7 +298,4 @@ namespace SHADE typedef SHHandleGenerator EntityHandleGenerator; -} - - -#endif +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h b/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h index f83d3052..913fca5a 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseBase.h @@ -43,7 +43,4 @@ namespace SHADE }; -} - - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h b/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h index 6dada871..8e096a37 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseSet.h @@ -350,6 +350,4 @@ namespace SHADE } }; -} - -#endif +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h b/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h index dbba0021..8f8814f6 100644 --- a/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h +++ b/SHADE_Engine/src/ECS_Base/General/SHSparseSetContainer.h @@ -242,6 +242,3 @@ namespace SHADE }; } - - -#endif diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h index dada878b..7d4e8c27 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h @@ -10,9 +10,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ - -#ifndef SH_SYSTEM_MANAGER_H -#define SH_SYSTEM_MANAGER_H +#pragma once #include @@ -119,8 +117,4 @@ namespace SHADE }; -} - - - -#endif +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 0d48c721..3a0694e2 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -8,10 +8,7 @@ consent of DigiPen Institute of Technology is prohibited. *********************************************************************/ -#ifndef SH_SYSTEM_H -#define SH_SYSTEM_H - - +#pragma once namespace SHADE { @@ -90,6 +87,4 @@ namespace SHADE }; -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h index 6f70f09b..531a62b0 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h @@ -1 +1,4 @@ #pragma once + + + From 4ccfd399af1d1936c2c04eb2a8d635327b7edd63 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Tue, 13 Sep 2022 19:46:51 +0800 Subject: [PATCH 26/36] Renderpass execution and descriptor set writes --- SHADE_Engine/SHADE_Engine.vcxproj | 4 +- SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + .../Editor/Backend/SHImGuiVulkanBackend.cpp | 28 +++- .../src/Editor/Backend/SHImGuiVulkanBackend.h | 5 +- .../Graphics/Commands/SHVkCommandBuffer.cpp | 5 + .../src/Graphics/Commands/SHVkCommandBuffer.h | 4 +- .../Descriptors/SHDescriptorSetUpdater.cpp | 84 +++++++++++ .../Descriptors/SHDescriptorSetUpdater.h | 60 ++++++++ .../Descriptors/SHVkDescriptorSetGroup.cpp | 107 +++++++++++++- .../Descriptors/SHVkDescriptorSetGroup.h | 132 ++++++++++-------- .../Descriptors/SHVkDescriptorSetLayout.cpp | 5 + .../Descriptors/SHVkDescriptorSetLayout.h | 1 + .../Graphics/Devices/SHVkLogicalDevice.cpp | 16 +++ .../src/Graphics/Devices/SHVkLogicalDevice.h | 3 + .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 2 - .../Graphics/RenderGraph/SHRenderGraph.cpp | 86 +++++++++++- .../src/Graphics/RenderGraph/SHRenderGraph.h | 51 ++++++- .../Graphics/Shaders/SHShaderReflected.cpp | 2 +- .../src/Graphics/Shaders/SHShaderReflected.h | 5 +- 19 files changed, 515 insertions(+), 87 deletions(-) create mode 100644 SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp create mode 100644 SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index c8dbf732..08f58da4 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -128,6 +128,7 @@ + @@ -206,6 +207,7 @@ + @@ -276,4 +278,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 1324473d..8e6c7d90 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -367,6 +367,7 @@ Tools + @@ -550,5 +551,6 @@ Tools + \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp index f00a2072..1ec09e05 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -40,7 +40,7 @@ namespace SHADE gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); } */ - static uint32_t __glsl_shader_vert_spv[] = + static std::vector __glsl_shader_vert_spv = { 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b, 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, @@ -97,7 +97,7 @@ namespace SHADE fColor = In.Color * texture(sTexture, In.UV.st); } */ - static uint32_t __glsl_shader_frag_spv[] = + static std::vector __glsl_shader_frag_spv = { 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b, 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, @@ -137,7 +137,7 @@ namespace SHADE std::array translate; }; - void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice) noexcept + void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex) noexcept { if (ImGui::GetCurrentContext() == nullptr) { @@ -189,7 +189,7 @@ namespace SHADE //Initialize instance auto instance = std::make_unique(); - instance->InitializePipeline(io); + instance->InitializePipeline(io, renderpass, subpassIndex); instance->lastFrameTime = std::chrono::high_resolution_clock::now(); io.UserData = instance.release(); @@ -303,7 +303,7 @@ namespace SHADE io.Fonts->TexID = nullptr; } - void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io) noexcept + void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept { SHVertexInputState vInputState{}; vInputState.AddBinding(false, true, @@ -314,7 +314,23 @@ namespace SHADE } ); - //Handle newPipeline = device->CreatePipeline() + Handle vs = device->CreateShaderModule(__glsl_shader_vert_spv, "main", vk::ShaderStageFlagBits::eVertex, "__glsl_shader_vert_spv"); + Handle fs = device->CreateShaderModule(__glsl_shader_frag_spv, "main", vk::ShaderStageFlagBits::eFragment, "__glsl_shader_frag_spv"); + + SHPipelineLayoutParams pipelineLayoutParams + { + .shaderModules = std::move (std::vector> + { + vs, + fs, + }), + }; + + Handle pipelineLayouot = device->CreatePipelineLayout(pipelineLayoutParams); + Handle newPipeline = device->CreatePipeline(pipelineLayouot, nullptr, renderpass, subpassIndex, SH_PIPELINE_TYPE::GRAPHICS); + newPipeline->GetPipelineState().SetVertexInputState(vInputState); + newPipeline->ConstructPipeline(); + } } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h index 387d173d..d6ebf84d 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -41,11 +41,10 @@ namespace SHADE std::vector fontsMipOffset; public: void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; - void InitializeRenderpass (void) noexcept; - void InitializePipeline(ImGuiIO& io) noexcept; + void InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept; }; public: - static void CreateInstance(Handle const& logicalDevice) noexcept; + static void CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex) noexcept; static void Render(void) noexcept; diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index 0cc9138e..5b2d15fe 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -202,6 +202,11 @@ namespace SHADE vkCommandBuffer.endRenderPass(); } + void SHVkCommandBuffer::NextSubpass(void) noexcept + { + vkCommandBuffer.nextSubpass(commandBufferType == SH_CMD_BUFFER_TYPE::PRIMARY ? vk::SubpassContents::eInline : vk::SubpassContents::eSecondaryCommandBuffers); + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index 932a0d47..d2171c25 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -96,11 +96,12 @@ namespace SHADE /*-----------------------------------------------------------------------*/ void Reset(void); - // Begins and Ends + // Begins, Ends and Nexts void BeginRecording (void) noexcept; void EndRecording (void) noexcept; void BeginRenderpass (Handle const& renderpassHdl, Handle const& framebufferHdl, vk::Offset2D offset = {0, 0}, vk::Extent2D extent = {0, 0}) noexcept; void EndRenderpass (void) noexcept; + void NextSubpass (void) noexcept; // 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; @@ -124,6 +125,7 @@ namespace SHADE std::vector const& imageMemoryBarriers ) const noexcept; + // Push Constant variable setting template void SetPushConstantVariable(std::string variableName, T const& data) noexcept diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp new file mode 100644 index 00000000..a1b1cbc2 --- /dev/null +++ b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp @@ -0,0 +1,84 @@ +#include "SHpch.h" +#include "SHDescriptorSetUpdater.h" + +namespace SHADE +{ + + SHDescriptorWriteInfo::SHDescriptorWriteInfo(SHDescriptorWriteInfo&& rhs) noexcept + : descImageInfos{ std::move(rhs.descImageInfos) } + , descBufferInfos{ std::move(rhs.descBufferInfos) } + , descTexelBufferInfos{std::move (rhs.descTexelBufferInfos)} + { + + } + + SHDescriptorWriteInfo::SHDescriptorWriteInfo(void) noexcept + : descImageInfos{} + , descBufferInfos{} + , descTexelBufferInfos{} + { + + } + + /***************************************************************************/ + /*! + + \brief + Links the write infos to the vulkan write descriptor sets. + + */ + /***************************************************************************/ + void SHDescriptorSetUpdater::LinkInfoToWriteDescSet(void) noexcept + { + for (uint32_t i = 0; i < writeInfos.size(); ++i) + { + writeDescSets[i].pImageInfo = writeInfos[i].descImageInfos.data(); + writeDescSets[i].pBufferInfo = writeInfos[i].descBufferInfos.data(); + writeDescSets[i].pTexelBufferView = writeInfos[i].descTexelBufferInfos.data(); + } + } + + SHDescriptorWriteInfo& SHDescriptorWriteInfo::operator=(SHDescriptorWriteInfo&& rhs) noexcept + { + if (&rhs == this) + return *this; + + descImageInfos = std::move(rhs.descImageInfos); + descBufferInfos = std::move(rhs.descBufferInfos); + descTexelBufferInfos = std::move(rhs.descTexelBufferInfos); + + return *this; + } + + + SHDescriptorSetUpdater::SHDescriptorSetUpdater(SHDescriptorSetUpdater&& rhs) noexcept + : writeInfos{ std::move(rhs.writeInfos) } + , writeHashMap {std::move (rhs.writeHashMap)} + { + + } + + SHDescriptorSetUpdater::SHDescriptorSetUpdater(void) noexcept + : writeInfos{} + , writeHashMap{} + { + + } + + std::vector const& SHDescriptorSetUpdater::GetWriteDescriptorSets(void) const noexcept + { + return writeDescSets; + } + + SHDescriptorSetUpdater& SHDescriptorSetUpdater::operator=(SHDescriptorSetUpdater&& rhs) noexcept + { + if (&rhs == this) + return *this; + + writeInfos = std::move (rhs.writeInfos); + writeHashMap = std::move (rhs.writeHashMap); + + return *this; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h new file mode 100644 index 00000000..7a5ae967 --- /dev/null +++ b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include "Graphics/SHVulkanIncludes.h" +#include "Graphics/Shaders/SHShaderReflected.h" + +namespace SHADE +{ + // Vulkan doesn't use all of the information when looking at a writeDescriptorSet. It all + // depends on the descriptor type. This struct plays it safe by having members that would + // accommodate all types of descriptors. + class SHDescriptorWriteInfo + { + //! When we want to update a descriptor that is an image, it goes in here + std::vector descImageInfos; + + //! When we want to update a descriptor that is a buffer, it goes in here + std::vector descBufferInfos; + + //! When we want to update a descriptor that is an texel buffer, it goes in here + std::vector descTexelBufferInfos; + + public: + SHDescriptorWriteInfo (void) noexcept; + SHDescriptorWriteInfo (SHDescriptorWriteInfo&& rhs) noexcept; + SHDescriptorWriteInfo& operator= (SHDescriptorWriteInfo&& rhs) noexcept; + + friend class SHVkDescriptorSetGroup; + friend class SHDescriptorSetUpdater; + }; + + class SHDescriptorSetUpdater + { + private: + //! When we want to update descriptor sets, this will get passed into vkUpdateDescriptorSets. + //! Each write will correspond to a binding from a set. If the binding is a variable + //! sized binding, pImageInfo (e.g.) will point to an array of vk::DescriptorImageInfo. + std::vector writeInfos; + + //! When we want to update a write, we need to use this to identify the index of the write. + std::unordered_map writeHashMap; + + //! We keep this here because we want this to be immediately passable to vkUpdateDescriptorSets + std::vector writeDescSets; + + void LinkInfoToWriteDescSet(void) noexcept; + + public: + SHDescriptorSetUpdater (void) noexcept; + SHDescriptorSetUpdater(SHDescriptorSetUpdater&& rhs) noexcept; + SHDescriptorSetUpdater& operator= (SHDescriptorSetUpdater&& rhs) noexcept; + + public: + std::vector const& GetWriteDescriptorSets (void) const noexcept; + + friend class SHVkDescriptorSetGroup; + }; +} + diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp index 6bdc5601..16143aa5 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp @@ -42,9 +42,11 @@ namespace SHADE { // Create the layout for each concurrent frame std::vector vkLayouts{ layouts.size() }; - for (auto& layout : layouts) + + //for (auto& layout : layouts) + for (uint32_t i = 0; i < layouts.size(); ++i) { - vkLayouts.push_back(layout->GetVkHandle()); + vkLayouts.push_back(layouts[i]->GetVkHandle()); } // Check for variable descriptor count @@ -67,6 +69,66 @@ namespace SHADE // allocate descriptor sets descSets = device->GetVkLogicalDevice().allocateDescriptorSets(DESC_SET_LAYOUT_CREATE_INFO); + + + + // Now we want to prepare the write descriptor sets for writing later. + for (uint32_t i = 0; i < layouts.size(); ++i) + { + auto const& bindings = layouts[i]->GetBindings(); + for (auto& binding : bindings) + { + BindingAndSetHash writeHash = binding.BindPoint; + writeHash |= static_cast(i) << 32; + + // new write for the binding + updater.writeInfos.emplace_back(); + updater.writeHashMap.try_emplace(writeHash, updater.writeInfos.size() - 1); + auto& writeInfo = updater.writeInfos.back(); + + updater.writeDescSets.emplace_back(); + auto& writeDescSet = updater.writeDescSets.back(); + + // Initialize info for write + writeDescSet.descriptorType = binding.Type; + writeDescSet.dstArrayElement = 0; + writeDescSet.dstSet = descSets[i]; + writeDescSet.dstBinding = binding.BindPoint; + + // Descriptor count for the write descriptor set. Usually this is set to 1, but if binding is variable sized, set to info passed in + uint32_t descriptorCount = (binding.flags & vk::DescriptorBindingFlagBits::eVariableDescriptorCount) ? variableDescCounts[i] : 1; + writeDescSet.descriptorCount = descriptorCount; + + switch (binding.Type) + { + //case vk::DescriptorType::eSampler: + //case vk::DescriptorType::eSampledImage: + case vk::DescriptorType::eCombinedImageSampler: + writeInfo.descImageInfos.resize(descriptorCount); + break; + //case vk::DescriptorType::eStorageImage: + // break; + case vk::DescriptorType::eUniformTexelBuffer: + case vk::DescriptorType::eStorageTexelBuffer: + case vk::DescriptorType::eUniformBuffer: + case vk::DescriptorType::eStorageBuffer: + writeInfo.descImageInfos.resize (descriptorCount); + break; + //case vk::DescriptorType::eUniformBufferDynamic: + // break; + //case vk::DescriptorType::eStorageBufferDynamic: + // break; + //case vk::DescriptorType::eInputAttachment: + // break; + //case vk::DescriptorType::eInlineUniformBlock: + // break; + default: + break; + } + } + } + // Link all the writeDescSet data for vkUpdateDescriptorSets to write to the linked descriptors + updater.LinkInfoToWriteDescSet(); } /***************************************************************************/ @@ -82,4 +144,45 @@ namespace SHADE if (!descSets.empty()) device->GetVkLogicalDevice().freeDescriptorSets(descPool->GetVkHandle(), descSets); } + + /***************************************************************************/ + /*! + + \brief + Modifies a descriptor write info. #NoteToSelf: This function does NOT + need to modify the writeDescSets. Those are already linked before. + + \param imageViewsAndSamplers + Image and view samplers + + */ + /***************************************************************************/ + void SHVkDescriptorSetGroup::ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector> const& imageViewsAndSamplers) noexcept + { + // Find the target writeDescSet + BindingAndSetHash writeHash = binding; + writeHash |= static_cast(set) << 32; + auto& writeInfo = updater.writeInfos[updater.writeHashMap.at(writeHash)]; + + if (imageViewsAndSamplers.size() > writeInfo.descImageInfos.size()) + { + SHLOG_ERROR("Attempting write too many descriptors into descriptor set. Failed to write to vk::WriteDescriptorSet. "); + } + + for (uint32_t i = 0; i < imageViewsAndSamplers.size(); ++i) + { + // write sampler and image view + auto& ivs = imageViewsAndSamplers[i]; + writeInfo.descImageInfos[i].imageView = ivs.first; + writeInfo.descImageInfos[i].sampler = ivs.second; + } + } + + + void SHVkDescriptorSetGroup::UpdateDescriptorSet(void) noexcept + { + device->UpdateDescriptorSets(updater.GetWriteDescriptorSets()); + } + + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h index b95859bb..9e311e9a 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h @@ -1,71 +1,89 @@ - #pragma once +#pragma once // Project Includes #include "Graphics/SHVulkanIncludes.h" #include "Resource/Handle.h" +#include "Graphics/Shaders/SHShaderReflected.h" +#include "SHDescriptorSetUpdater.h" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Forward Declarations */ - /*---------------------------------------------------------------------------------*/ - class SHVkLogicalDevice; - class SHVkDescriptorPool; - class SHVkDescriptorSetLayout; + /*---------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*---------------------------------------------------------------------------------*/ + class SHVkLogicalDevice; + class SHVkDescriptorPool; + class SHVkDescriptorSetLayout; - /*---------------------------------------------------------------------------------*/ - /* Type Definitions */ - /*---------------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + /// + /// + /// + class SHVkDescriptorSetGroup + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constructor/Destructors */ + /*-----------------------------------------------------------------------------*/ /// - /// + /// Constructs a Descriptor Set with the specified layout using the specified + /// pool meant for use with the specified surface. This Set will be created with + /// multiple Vulkan Descriptor Set objects based on the max number of concurrent + /// frames for the specified surface. /// - class SHVkDescriptorSetGroup - { - public: - /*-----------------------------------------------------------------------------*/ - /* Constructor/Destructors */ - /*-----------------------------------------------------------------------------*/ - /// - /// Constructs a Descriptor Set with the specified layout using the specified - /// pool meant for use with the specified surface. This Set will be created with - /// multiple Vulkan Descriptor Set objects based on the max number of concurrent - /// frames for the specified surface. - /// - /// Vulkan logical device used to create the Set. - /// Descriptor Pool used to create the Set. - /// Descriptor Set Layout to create the Set with. - SHVkDescriptorSetGroup(Handle deviceHdl, Handle pool, - std::vector> const& layouts, - std::vector const& variableDescCounts); - SHVkDescriptorSetGroup(const SHVkDescriptorSetGroup&) = delete; - SHVkDescriptorSetGroup(SHVkDescriptorSetGroup&& rhs) noexcept = default; - /// - /// Destructor which will unload and deallocate all resources for this Descriptor Set. - /// - ~SHVkDescriptorSetGroup() noexcept; + /// Vulkan logical device used to create the Set. + /// Descriptor Pool used to create the Set. + /// Descriptor Set Layout to create the Set with. + SHVkDescriptorSetGroup(Handle deviceHdl, Handle pool, + std::vector> const& layouts, + std::vector const& variableDescCounts); + SHVkDescriptorSetGroup(const SHVkDescriptorSetGroup&) = delete; + SHVkDescriptorSetGroup(SHVkDescriptorSetGroup&& rhs) noexcept = default; + /// + /// Destructor which will unload and deallocate all resources for this Descriptor Set. + /// + ~SHVkDescriptorSetGroup() noexcept; - /*-----------------------------------------------------------------------------*/ - /* Overloaded Operators */ - /*-----------------------------------------------------------------------------*/ - SHVkDescriptorSetGroup& operator=(const SHVkDescriptorSetGroup&) = delete; - SHVkDescriptorSetGroup& operator=(SHVkDescriptorSetGroup&& rhs) noexcept = default; + /*-----------------------------------------------------------------------------*/ + /* Overloaded Operators */ + /*-----------------------------------------------------------------------------*/ + SHVkDescriptorSetGroup& operator=(const SHVkDescriptorSetGroup&) = delete; + SHVkDescriptorSetGroup& operator=(SHVkDescriptorSetGroup&& rhs) noexcept = default; - /*-----------------------------------------------------------------------------*/ - /* Getter Functions */ - /*-----------------------------------------------------------------------------*/ - /// - /// Retrieves the handle to the Vulkan Descriptor Set handle. - /// - /// Handle to the Vulkan Descriptor Set. - [[nodiscard]] - inline const std::vector& GetVkHandle() { return descSets; } + /*-----------------------------------------------------------------------------*/ + /* Descriptor set writing */ + /*-----------------------------------------------------------------------------*/ + void ModifyWriteDescImage (uint32_t set, uint32_t binding, std::vector> const& imageViewsAndSamplers) noexcept; + void UpdateDescriptorSet (void) noexcept; - private: - /*-----------------------------------------------------------------------------*/ - /* Data Members */ - /*-----------------------------------------------------------------------------*/ - Handle device; - Handle descPool; - std::vector descSets; - }; + /*-----------------------------------------------------------------------------*/ + /* Getter Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Retrieves the handle to the Vulkan Descriptor Set handle. + /// + /// Handle to the Vulkan Descriptor Set. + [[nodiscard]] + inline const std::vector& GetVkHandle() { return descSets; } + private: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + //! Device required to allocate descriptor sets + Handle device; + + //! Descriptor pool to allocate descriptor sets + Handle descPool; + + //! Descriptor sets + std::vector descSets; + + //! for updating descriptor sets. We want to cache this so that we don't create the + //! write structs at runtime. + SHDescriptorSetUpdater updater; + + }; } diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp index 36eaa8e8..da1a3645 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp @@ -85,6 +85,11 @@ namespace SHADE device->GetVkLogicalDevice().destroyDescriptorSetLayout(setLayout); } + std::vector const& SHVkDescriptorSetLayout::GetBindings(void) const noexcept + { + return layoutDesc; + } + SHVkDescriptorSetLayout& SHVkDescriptorSetLayout::operator=(SHVkDescriptorSetLayout&& rhs) noexcept { if (&rhs == this) diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h index 590fd787..1acba189 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h @@ -96,6 +96,7 @@ namespace SHADE /// /// Handle to the Vulkan Descriptor Set Layout handle. inline const vk::DescriptorSetLayout& GetVkHandle() const { return setLayout; } + std::vector const& GetBindings (void) const noexcept; private: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index 69e76246..da4947f2 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -552,6 +552,22 @@ namespace SHADE return SHVkInstance::GetResourceManager().Create(GetHandle()); } + /***************************************************************************/ + /*! + + \brief + Writes to descriptor sets. + + \param writeDescSets + Descriptor sets to write to. + + */ + /***************************************************************************/ + void SHVkLogicalDevice::UpdateDescriptorSets(std::vector const& writeDescSets) noexcept + { + vkLogicalDevice.updateDescriptorSets(writeDescSets, {}); + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index d50d27f0..c359d1d3 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -38,6 +38,7 @@ namespace SHADE class SHVkFramebuffer; class SHVkImageView; class SHShaderBlockInterface; + class SHVkDescriptorSetGroup; /***************************************************************************/ /*! @@ -179,6 +180,8 @@ namespace SHADE Handle CreateFence (void) const noexcept; Handle CreateSemaphore (void) const noexcept; + void UpdateDescriptorSets (std::vector const& writeDescSets) noexcept; + /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index d9761adf..4d4ca4f4 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -84,8 +84,6 @@ namespace SHADE //commandBuffers[i] = commandPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); // works } - - descPool = device->CreateDescriptorPools(); diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index 68af135f..8a7ce8a0 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -209,9 +209,10 @@ namespace SHADE */ /***************************************************************************/ - SHRenderGraphNode::SHSubpass::SHSubpass(std::unordered_map const* mapping, std::unordered_map> const* resources) noexcept + SHRenderGraphNode::SHSubpass::SHSubpass(Handle const& parent, std::unordered_map const* mapping, std::unordered_map> const* resources) noexcept : resourceAttachmentMapping{ mapping } , ptrToResources{ resources } + , parentNode{ parent } { } @@ -333,6 +334,38 @@ namespace SHADE inputReferences.push_back({ resourceAttachmentMapping->at(ptrToResources->at(resourceToReference).GetId().Raw), vk::ImageLayout::eShaderReadOnlyOptimal }); } + void SHRenderGraphNode::SHSubpass::Execute(Handle& commandBuffer) noexcept + { + // Draw all the batches + + // Draw all the exterior draw calls + for (auto& drawCall : exteriorDrawCalls) + { + drawCall(commandBuffer); + } + } + + void SHRenderGraphNode::SHSubpass::AddExteriorDrawCalls(std::function&)> const& newDrawCall) noexcept + { + exteriorDrawCalls.push_back(newDrawCall); + } + + /***************************************************************************/ + /*! + + \brief + Getter for parent renderpass. + + \return + Returns the parent renderpass the subpass belongs to. + + */ + /***************************************************************************/ + Handle const& SHRenderGraphNode::SHSubpass::GetParentNode(void) const noexcept + { + return parentNode; + } + /***************************************************************************/ /*! @@ -512,11 +545,26 @@ namespace SHADE } // Add subpass to container and create mapping for it - subpasses.emplace_back(resourceManager.Create(&resourceAttachmentMapping, ptrToResources)); - subpassIndexing.try_emplace(subpassName, subpasses.size() - 1); + subpasses.emplace_back(resourceManager.Create(GetHandle(), &resourceAttachmentMapping, ptrToResources)); + subpassIndexing.try_emplace(subpassName, static_cast(subpasses.size()) - 1u); return subpasses.at(subpassIndexing[subpassName]); } + void SHRenderGraphNode::Execute(Handle& commandBuffer, uint32_t frameIndex) noexcept + { + commandBuffer->BeginRenderpass(renderpass, framebuffers[frameIndex]); + + for (auto& subpass : subpasses) + { + subpass->Execute(commandBuffer); + + // Go to next subpass + commandBuffer->NextSubpass(); + } + + commandBuffer->EndRenderpass(); + } + /***************************************************************************/ /*! @@ -560,7 +608,7 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::AddResource(std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w /*= static_cast(-1)*/, uint32_t h /*= static_cast(-1)*/, vk::Format format/* = vk::Format::eB8G8R8A8Unorm*/, uint32_t levels /*= 1*/, vk::ImageCreateFlagBits createFlags /*= {}*/) + void SHRenderGraph::AddResource(std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w /*= static_cast(-1)*/, uint32_t h /*= static_cast(-1)*/, vk::Format format/* = vk::Format::eB8G8R8A8Unorm*/, uint8_t levels /*= 1*/, vk::ImageCreateFlagBits createFlags /*= {}*/) { // If we set to if (w == static_cast(-1) && h == static_cast(-1)) @@ -832,6 +880,20 @@ namespace SHADE } } + /***************************************************************************/ + /*! + + \brief + Configures command pools and command buffers. + + */ + /***************************************************************************/ + void SHRenderGraph::ConfigureCommands(void) noexcept + { + commandPool = logicalDeviceHdl->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); + commandBuffers.resize(static_cast(swapchainHdl->GetNumImages())); + } + /***************************************************************************/ /*! @@ -929,7 +991,7 @@ namespace SHADE } nodes.emplace_back(resourceManager.Create(resourceManager, logicalDeviceHdl, swapchainHdl, std::move(resources), std::move(predecessors), &graphResources)); - nodeIndexing.emplace(nodeName, nodes.size() - 1); + nodeIndexing.emplace(nodeName, static_cast(nodes.size()) - 1u); return nodes.at(nodeIndexing[nodeName]); } @@ -952,6 +1014,20 @@ namespace SHADE ConfigureSubpasses(); ConfigureRenderpasses(); ConfigureFramebuffers(); + ConfigureCommands(); + } + + void SHRenderGraph::Execute(uint32_t frameIndex) noexcept + { + auto& cmdBuffer = commandBuffers[frameIndex]; + cmdBuffer->BeginRecording(); + + for (auto& node : nodes) + { + node->Execute(commandBuffers[frameIndex], frameIndex); + } + + cmdBuffer->EndRenderpass(); } Handle SHRenderGraph::GetNode(std::string const& nodeName) const noexcept diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index dc6b7c42..ea913baa 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -15,6 +15,8 @@ namespace SHADE class SHVkImage; class SHVkImageView; class SHVkFramebuffer; + class SHVkCommandPool; + class SHVkCommandBuffer; // Used for attachment description creation for renderpass node enum class SH_ATT_DESC_TYPE @@ -72,25 +74,44 @@ namespace SHADE friend class SHRenderGraph; }; - class SHRenderGraphNode + class SHRenderGraphNode : public ISelfHandle { public: class SHSubpass { public: - SHSubpass(std::unordered_map const* mapping, std::unordered_map> const* ptrToResources) noexcept; + /*-----------------------------------------------------------------------*/ + /* CTORS AND DTORS */ + /*-----------------------------------------------------------------------*/ + SHSubpass(Handle const& parent, std::unordered_map const* mapping, std::unordered_map> const* ptrToResources) noexcept; SHSubpass(SHSubpass&& rhs) noexcept; SHSubpass& operator=(SHSubpass&& rhs) noexcept; + /*-----------------------------------------------------------------------*/ + /* PUBLIC MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + // Preparation functions void AddColorOutput (std::string resourceToReference) noexcept; void AddDepthOutput (std::string resourceToReference, SH_ATT_DESC_TYPE attachmentDescriptionType = SH_ATT_DESC_TYPE::DEPTH_STENCIL) noexcept; void AddInput (std::string resourceToReference) noexcept; + + // Runtime functions + void Execute (Handle& commandBuffer) noexcept; + void AddExteriorDrawCalls (std::function&)> const& newDrawCall) noexcept; + + /*-----------------------------------------------------------------------*/ + /* GETTERS AND SETTERS */ + /*-----------------------------------------------------------------------*/ + Handle const& GetParentNode (void) const noexcept; private: /*---------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ /*---------------------------------------------------------------------*/ + //! The parent renderpass that this subpass belongs to + Handle parentNode; + //! Color attachments std::vector colorReferences; @@ -106,6 +127,14 @@ namespace SHADE //! Pointer to resources in the render graph (for getting handle IDs) std::unordered_map> const* ptrToResources; + //! Sometimes there exists entities that we want to render onto a render target + //! but don't want it to come from the batching system. An example would be ImGUI. + //! For these entities we want to link a function from the outside and draw them + //! after we draw everything from the batch. Because of this, these draw calls + //! are always the last things drawn, so DO NOT USE THIS FUNCTIONALITY FOR ANYTHING + //! COMPLEX. + std::vector&)>> exteriorDrawCalls; + friend class SHRenderGraphNode; friend class SHRenderGraph; }; @@ -179,6 +208,7 @@ namespace SHADE /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ Handle AddSubpass (std::string subpassName) noexcept; + void Execute (Handle& commandBuffer, uint32_t frameIndex) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ @@ -198,6 +228,7 @@ namespace SHADE void ConfigureSubpasses (void) noexcept; void ConfigureRenderpasses (void) noexcept; void ConfigureFramebuffers (void) noexcept; + void ConfigureCommands (void) noexcept; /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ @@ -219,6 +250,13 @@ namespace SHADE //! Resource library for graph handles ResourceManager resourceManager; + //! Command pool for the render graph + Handle commandPool; + + //! Command buffers for the render graph + std::vector> commandBuffers; + + public: /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ @@ -228,10 +266,11 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle const& logicalDevice, Handle const& swapchain) noexcept; - void AddResource(std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint32_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); - Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; - void Generate (void) noexcept; + void Init (Handle const& logicalDevice, Handle const& swapchain) noexcept; + void AddResource (std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); + Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; + void Generate (void) noexcept; + void Execute (uint32_t frameIndex) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ diff --git a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.cpp b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.cpp index 8beb7d98..1e5fb074 100644 --- a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.cpp +++ b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.cpp @@ -189,7 +189,7 @@ namespace SHADE Handle SHShaderDescriptorBindingInfo::GetShaderBlockInterface(uint32_t set, uint32_t binding) const noexcept { - SHShaderDescriptorBindingInfo::BindingAndSetHash hash = binding; + BindingAndSetHash hash = binding; hash |= static_cast(set) << 32; if (blockInterfaces.contains(hash)) return blockInterfaces.at(hash); diff --git a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h index 1250b54f..88c7a2e1 100644 --- a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h +++ b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h @@ -9,11 +9,10 @@ namespace SHADE { + using BindingAndSetHash = uint64_t; + struct SHShaderDescriptorBindingInfo { - public: - using BindingAndSetHash = uint64_t; - private: /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ From b7cd1bed48c0daeada4df0ece694fea8ce7fa6fe Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Wed, 14 Sep 2022 10:32:50 +0800 Subject: [PATCH 27/36] Added System Routines --- SHADE_Engine/SHADE_Engine.vcxproj | 4 ++ SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 ++ .../src/ECS_Base/Managers/SHSystemManager.cpp | 35 +++++++++++-- .../src/ECS_Base/Managers/SHSystemManager.h | 40 +++++++++------ SHADE_Engine/src/ECS_Base/SHECSMacros.h | 4 +- .../ECS_Base/System/SHFixedSystemRoutine.cpp | 20 ++++++++ .../ECS_Base/System/SHFixedSystemRoutine.h | 31 ++++++++++++ .../src/ECS_Base/System/SHRoutineStats.h | 27 ++++++++++ SHADE_Engine/src/ECS_Base/System/SHSystem.h | 17 +++---- .../src/ECS_Base/System/SHSystemRoutine.cpp | 21 ++++++++ .../src/ECS_Base/System/SHSystemRoutine.h | 49 +++++++++++++++++++ 11 files changed, 224 insertions(+), 28 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp create mode 100644 SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h create mode 100644 SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h create mode 100644 SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 1831bff6..d84f3c05 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -114,6 +114,8 @@ + + @@ -200,6 +202,8 @@ + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 70a5b407..8fc412c5 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -386,6 +386,8 @@ Tools + + @@ -587,5 +589,7 @@ Tools + + \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp index 5b861161..acf7cba3 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp @@ -14,10 +14,14 @@ #include "SHSystemManager.h" #include +#include +#include +#include + namespace SHADE { SHSystemManager::SystemContainer SHSystemManager::systemContainer; - + SHSystemManager::SystemRoutineContainer SHSystemManager::systemRoutineContainer; void SHSystemManager::Init() noexcept { @@ -30,11 +34,36 @@ namespace SHADE } } + void SHSystemManager::RunRoutines(bool editorPause) noexcept + { + for (auto& routine : systemRoutineContainer) + { + if (editorPause == true) + { + if (routine.get()->IsRunInEditorPause) + { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + routine.get()->Execute(0.0); + std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); + routine.get()->stats.executionTime = std::chrono::duration(end - start).count(); + } + } + else + { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + routine.get()->Execute(0.0); + std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); + routine.get()->stats.executionTime = std::chrono::duration(end - start).count(); + } + } + } + + void SHSystemManager::Exit() noexcept { - for (auto& system : systemContainer) + for (SystemContainer::reverse_iterator it = systemContainer.rbegin(); it != systemContainer.rend(); ++it) { - system.second->Exit(); + (*it).second->Exit(); //delete system.second; } diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h index 7d4e8c27..616a8db3 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h @@ -13,30 +13,29 @@ #pragma once -#include +#include #include #include #include #include #include "../System/SHSystem.h" #include "../General/SHFamily.h" +#include "../System/SHSystemRoutine.h" namespace SHADE { - typedef uint32_t SystemTypeID; - typedef uint32_t SystemVersionID; - typedef uint64_t SystemID; + typedef SHFamilyID SystemFamily; class SHSystemManager { //type definition for the container we use to store our system - using SystemContainer = std::unordered_map>; - + using SystemContainer = std::map>; + using SystemRoutineContainer = std::vector>; private: static SystemContainer systemContainer; - + static SystemRoutineContainer systemRoutineContainer; public: /*!************************************************************************* @@ -47,12 +46,12 @@ namespace SHADE ~SHSystemManager() = delete; /************************************************************************** - * \brief + * \brief * Create a system of type T and map it to a name. * throws an error if a system with the same name already exists. * \param name * name of the system - * \return + * \return * none ***************************************************************************/ template @@ -61,7 +60,7 @@ namespace SHADE SystemTypeID typeID = SystemFamily::GetID(); SystemVersionID version = 0; - SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; + SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; while (systemContainer.find(id) != systemContainer.end()) { ++version; @@ -73,11 +72,11 @@ namespace SHADE } /************************************************************************** - * \brief + * \brief * Get a pointer to the System with a specified name. * \param name * Name of the system in the map - * \return + * \return * Base System pointer. ***************************************************************************/ template @@ -97,13 +96,26 @@ namespace SHADE } /************************************************************************** - * \brief + * \brief * Call the Init function of all systems. - * \return + * \return * none ***************************************************************************/ static void Init() noexcept; + static void RunRoutines(bool editorPause) noexcept; + + template + static void RegisterRoutine(SystemVersionID version = 0) noexcept + { + SHSystem* system = GetSystem(version); + if (system == nullptr) + return; + systemRoutineContainer.emplace_back(std::make_unique()); + + } + + /************************************************************************** * \brief * Call the Exit function of all systems. diff --git a/SHADE_Engine/src/ECS_Base/SHECSMacros.h b/SHADE_Engine/src/ECS_Base/SHECSMacros.h index 0a8971e7..02615ca4 100644 --- a/SHADE_Engine/src/ECS_Base/SHECSMacros.h +++ b/SHADE_Engine/src/ECS_Base/SHECSMacros.h @@ -9,7 +9,9 @@ typedef uint32_t EntityID; typedef uint16_t EntityIndex; typedef uint32_t ComponentTypeID; - +typedef uint32_t SystemTypeID; +typedef uint32_t SystemVersionID; +typedef uint64_t SystemID; diff --git a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp new file mode 100644 index 00000000..2c97bfb4 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp @@ -0,0 +1,20 @@ +#include "SHFixedSystemRoutine.h" +#include "../SHECSMacros.h" + +namespace SHADE +{ + + void SHFixedSystemRoutine::Execute(double dt) + { + accumulatedTime += dt; + int counter = 0; + while (accumulatedTime >= fixedTimeStep) + { + ++counter; + accumulatedTime -= fixedTimeStep; + FixedExecute(fixedTimeStep); + } + stats.numSteps = counter; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h new file mode 100644 index 00000000..d9a2b510 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.h @@ -0,0 +1,31 @@ +#pragma once + + +#include "SHSystemRoutine.h" +#define DEFAULT_FIXED_STEP 1.0/60.0 + +namespace SHADE +{ + class SHFixedSystemRoutine: public SHSystemRoutine + { + private: + double accumulatedTime; + double fixedTimeStep; + + protected: + SHFixedSystemRoutine(double timeStep = DEFAULT_FIXED_STEP, std::string routineName = "Default Fixed Routine Name", bool editorPause = false) + :SHSystemRoutine(routineName, editorPause), accumulatedTime(0.0), fixedTimeStep(timeStep){} + + + + public: + ~SHFixedSystemRoutine() = default; + + virtual void Execute(double dt) noexcept; + + virtual void FixedExecute(double dt) noexcept {}; + + }; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h new file mode 100644 index 00000000..e294264a --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +namespace SHADE +{ + struct SHRoutineStats + { + SHRoutineStats(std::string name) + :name(name) + { + } + std::string name; + double executionTime; + int numSteps{1}; + + friend std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats); + }; + + std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats) + { + os << stats.name << ": Execution Time: " << stats.executionTime << " Number of steps: " << stats.numSteps << std::endl; + } + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index 3a0694e2..a2964a91 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -37,13 +37,13 @@ namespace SHADE ***************************************************************************/ virtual void Init() = 0; - /*!************************************************************************* - * \brief - * Pure virtual Run function. Derived class must implement this - * \param dt - * Delta time - ***************************************************************************/ - virtual void Run(float dt) = 0; + ///*!************************************************************************* + // * \brief + // * Pure virtual Run function. Derived class must implement this + // * \param dt + // * Delta time + //***************************************************************************/ + //virtual void Run(float dt) = 0; /*!************************************************************************* * \brief @@ -75,10 +75,7 @@ namespace SHADE } - void Run(float dt) - { - } void Exit() { diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp new file mode 100644 index 00000000..83107c60 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp @@ -0,0 +1,21 @@ +#include "SHSystemRoutine.h" + +namespace SHADE +{ + + SHSystem* SHSystemRoutine::GetSystem() const noexcept + { + return system; + } + + std::string const SHSystemRoutine::GetName()const noexcept + { + return name; + } + + SHRoutineStats const& SHSystemRoutine::GetStats()const noexcept + { + return stats; + } + +} diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h index 531a62b0..cdb62438 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.h @@ -1,4 +1,53 @@ + + + #pragma once +#include "../SHECSMacros.h" +#include "SHRoutineStats.h" +#include "SHSystem.h" +#include + + +namespace SHADE +{ + + class SHSystemManager; + + + class SHSystemRoutine + { + friend class SHSystemManager; + protected: + + SHSystemRoutine(std::string routineName = "Default Routine Name", bool editorPause = false) + :system(nullptr), name(routineName), stats(routineName),IsRunInEditorPause(editorPause){}; + + + SHSystem* system; + std::string name; + SHRoutineStats stats; + + //Whether or not this routine should run when the editor is still in pause + bool IsRunInEditorPause; + + + public: + ~SHSystemRoutine() = default; + + + SHSystem* GetSystem()const noexcept; + std::string const GetName() const noexcept; + SHRoutineStats const& GetStats()const noexcept; + + virtual void Execute(double dt) noexcept {}; + }; + + + + +} + + From fdc8a61c1d8f190d2a283cf726f8fec060d5966e Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Wed, 14 Sep 2022 16:01:40 +0800 Subject: [PATCH 28/36] Added SHWindow to SHImguiWindow --- .../Editor/Backend/SHImGuiVulkanBackend.cpp | 37 ++++++++++++++++--- .../src/Editor/Backend/SHImGuiVulkanBackend.h | 18 ++++----- .../src/Graphics/Windowing/SHWindow.cpp | 2 +- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp index 1ec09e05..740ae683 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -137,7 +137,7 @@ namespace SHADE std::array translate; }; - void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex) noexcept + void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex, SHWindow& mainWindow) noexcept { if (ImGui::GetCurrentContext() == nullptr) { @@ -189,6 +189,7 @@ namespace SHADE //Initialize instance auto instance = std::make_unique(); + instance->window = mainWindow; instance->InitializePipeline(io, renderpass, subpassIndex); instance->lastFrameTime = std::chrono::high_resolution_clock::now(); io.UserData = instance.release(); @@ -200,9 +201,10 @@ namespace SHADE void SHImGuiVulkanBackend::Render(void) noexcept { + GETINSTANCE ImGui::Render(); ImDrawData* drawData = ImGui::GetDrawData(); - //TODO: instance draw data + //instance.Render(io, drawData, ) } void SHImGuiVulkanBackend::CreateChildWindow(ImGuiViewport* viewport) noexcept @@ -210,6 +212,7 @@ namespace SHADE GETINSTANCE auto& info = *new SHImGuiWindow{ instance.device }; + info.window.Create({}, instance.window.GetHWND()); viewport->RendererUserData = &info; } @@ -217,7 +220,9 @@ namespace SHADE { GETINSTANCE - auto info = reinterpret_cast(viewport->RendererUserData); + auto info = reinterpret_cast(viewport->RendererUserData); + info->window.Close(); + info->window.Destroy(); delete info; viewport->RendererUserData = nullptr; } @@ -230,20 +235,37 @@ namespace SHADE void SHImGuiVulkanBackend::SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept { - + auto& info = *reinterpret_cast(viewport->RendererUserData); + SetWindowPos + ( + info.window.GetHWND(), + HWND_TOPMOST, + -1, + -1, + static_cast(size.x), + static_cast(size.y), + SWP_NOMOVE | SWP_NOZORDER + ); } ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept { - return {}; + auto& info = *reinterpret_cast(viewport->RendererUserData); + auto [x, y] = info.window.GetWindowSize(); + return {static_cast(x), static_cast(y)}; } void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept { + auto& info = *reinterpret_cast(viewport->RendererUserData); + info.window.SetPosition(size.x, size.y); } void SHImGuiVulkanBackend::RenderChildWindow(ImGuiViewport* viewport, void*) noexcept { + GETINSTANCE; + auto& info = *reinterpret_cast(viewport->RendererUserData); + //info.Render(io, viewport->DrawData, instance);; } void SHImGuiVulkanBackend::ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept @@ -272,6 +294,11 @@ namespace SHADE } } + void SHImGuiVulkanBackend::SHImGuiWindow::Render(ImGuiIO& io, ImDrawData* drawData, + Handle descriptorSetGroup) + { + + } void SHImGuiVulkanBackend::SHBreachInstance::CreateFontsTexture(Handle image, ImGuiIO& io) noexcept diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h index d6ebf84d..65b88ccf 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -1,15 +1,16 @@ #pragma once #include -#include +#include "Graphics/Instance/SHVkInstance.h" #include "Graphics/Buffers/SHVkBuffer.h" #include "Graphics/Images/SHVkImage.h" -#include -#include -#include +#include "Graphics/Devices/SHVkPhysicalDevice.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/Pipeline/SHVkPipeline.h" #include "Graphics/Swapchain/SHVkSwapchain.h" -#include +#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" #include "Graphics/Debugging/SHVulkanDebugUtil.h" +#include "Graphics/Windowing/SHWindow.h" namespace SHADE { @@ -21,15 +22,14 @@ namespace SHADE { Handle vertexBuffer; Handle indicesBuffer; - }; - Handle device; + SHWindow window; std::array primitiveBuffers; public: void InitializeBuffers(void) noexcept; - + void Render(ImGuiIO& io, ImDrawData* drawData, Handle descriptorSetGroup); }; struct SHBreachInstance : public SHImGuiWindow @@ -44,7 +44,7 @@ namespace SHADE void InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept; }; public: - static void CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex) noexcept; + static void CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex, SHWindow& mainWindow) noexcept; static void Render(void) noexcept; diff --git a/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp b/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp index 6ac6672b..957ffc34 100644 --- a/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp +++ b/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp @@ -222,7 +222,7 @@ namespace SHADE return true; { MSG Message; - while (PeekMessageW(&Message, NULL, 0, 0, PM_REMOVE)) + while (PeekMessageW(&Message, wndHWND, 0, 0, PM_REMOVE)) { if (WM_QUIT == Message.message) { From 5a1abe6530cc4ade97ca5663a7ff99b5cd158c2c Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 14 Sep 2022 19:18:54 +0800 Subject: [PATCH 29/36] Auto stash before merge of "SP3-4-editor" and "origin/SP3-4-editor" --- .../Editor/Backend/SHImGuiVulkanBackend.cpp | 35 ++++++++++++++++++- .../src/Editor/Backend/SHImGuiVulkanBackend.h | 24 +++++++++++-- .../src/Graphics/Images/SHVkSampler.cpp | 6 ++++ .../src/Graphics/Images/SHVkSampler.h | 26 ++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp create mode 100644 SHADE_Engine/src/Graphics/Images/SHVkSampler.h diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp index 740ae683..676625bb 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp @@ -199,7 +199,7 @@ namespace SHADE viewport->PlatformHandle = io.UserData; } - void SHImGuiVulkanBackend::Render(void) noexcept + void SHImGuiVulkanBackend::Render(Handle const& commandBuffer) noexcept { GETINSTANCE ImGui::Render(); @@ -324,14 +324,22 @@ namespace SHADE fontsMipOffset.push_back(0); + // Create image. Data should be in staging only now fontsTexture = device->CreateImage(createParams, pixels, width * height * sizeof(uint32_t), fontsMipOffset, VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, {}); + // TODO: Copy the image to device memory here + // 1. Start record + // 2. Do transfer + // 3. End record + // 4. execute cmd buffer + // We could put here the texture id if we wanted io.Fonts->TexID = nullptr; } void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept { + // Vertex input state SHVertexInputState vInputState{}; vInputState.AddBinding(false, true, { @@ -341,9 +349,11 @@ namespace SHADE } ); + // Shaders Handle vs = device->CreateShaderModule(__glsl_shader_vert_spv, "main", vk::ShaderStageFlagBits::eVertex, "__glsl_shader_vert_spv"); Handle fs = device->CreateShaderModule(__glsl_shader_frag_spv, "main", vk::ShaderStageFlagBits::eFragment, "__glsl_shader_frag_spv"); + // pipeline layout initialize shaders SHPipelineLayoutParams pipelineLayoutParams { .shaderModules = std::move (std::vector> @@ -353,11 +363,34 @@ namespace SHADE }), }; + // Create pipeline layout Handle pipelineLayouot = device->CreatePipelineLayout(pipelineLayoutParams); + + // Create pipeline (but dont construct it yet) Handle newPipeline = device->CreatePipeline(pipelineLayouot, nullptr, renderpass, subpassIndex, SH_PIPELINE_TYPE::GRAPHICS); + + // Set the vertex input state newPipeline->GetPipelineState().SetVertexInputState(vInputState); + + // Actually construct the pipeline newPipeline->ConstructPipeline(); + SHVkDescriptorPool::Config config + { + .MaxSets = 10, + .Flags = {}, + }; + + // Create a descriptor pool + descriptorPool = device->CreateDescriptorPools(config); + + // Allocate descriptor sets required for the fonts + descriptorSetGroup = + } + + void SHImGuiVulkanBackend::SHBreachInstance::Render(Handle const& commandBuffer, ImGuiIO& io, ImDrawData* draw_data, Handle const& descriptorSetGroup) noexcept + { + } } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h index 65b88ccf..47f31e4c 100644 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h @@ -23,8 +23,13 @@ namespace SHADE Handle vertexBuffer; Handle indicesBuffer; }; + //Required to create stuff Handle device; + + // Window of the imgui window SHWindow window; + + // Buffers required to draw the imgui entities std::array primitiveBuffers; public: @@ -34,18 +39,33 @@ namespace SHADE struct SHBreachInstance : public SHImGuiWindow { + // pipeline/shaders used to render imgui entities Handle pipeline; - Handle renderpass; + + // Last frame time std::chrono::time_point lastFrameTime; + + // Fonts texture Handle fontsTexture; + + // mip maps for font image (should just contain 1) std::vector fontsMipOffset; + + // Descriptor pool rquired to allocate descriptor sets + Handle descriptorPool; + + // Descriptor set required to store font texture + Handle descriptorSetGroup; + public: void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; void InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept; + void Render (Handle const& commandBuffer, ImGuiIO& io, ImDrawData* draw_data, Handle const& descriptorSetGroup) noexcept; + }; public: static void CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex, SHWindow& mainWindow) noexcept; - static void Render(void) noexcept; + static void Render(Handle const& commandBuffer) noexcept; static void EnableDocking(void) noexcept; diff --git a/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp b/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp new file mode 100644 index 00000000..f6117022 --- /dev/null +++ b/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp @@ -0,0 +1,6 @@ +#include "SHVkSampler.h" + +namespace SHADE +{ + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Images/SHVkSampler.h b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h new file mode 100644 index 00000000..5c6fcaad --- /dev/null +++ b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h @@ -0,0 +1,26 @@ +#pragma once + +#include "Graphics/SHVulkanIncludes.h" + +namespace SHADE +{ + struct SHVkSamplerParams + { + vk::Filter minFilter; + vk::Filter maxFilter; + }; + + class SHVkSampler + { + private: + //! The vulkan sampler handler + vk::Sampler vkSampler; + + public: + SHVkSampler () noexcept; + SHVkSampler (SHVkSampler&& rhs) noexcept; + SHVkSampler&& operator=(SHVkSampler&& rhs) noexcept; + + }; +} + From 7459e198462c962e6fc2adea24a77f722e0b4561 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 14 Sep 2022 19:19:24 +0800 Subject: [PATCH 30/36] Changes --- SHADE_Engine/SHADE_Engine.vcxproj | 2 ++ SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 ++ SHADE_Engine/src/Graphics/Images/SHVkSampler.h | 1 + 3 files changed, 5 insertions(+) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 08f58da4..694485a8 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -139,6 +139,7 @@ + @@ -217,6 +218,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 8e6c7d90..ebf959ad 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -368,6 +368,7 @@ Tools + @@ -552,5 +553,6 @@ Tools + \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Images/SHVkSampler.h b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h index 5c6fcaad..50603d69 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkSampler.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h @@ -8,6 +8,7 @@ namespace SHADE { vk::Filter minFilter; vk::Filter maxFilter; + vk::Filter maxFilter; }; class SHVkSampler From 921680d472192196bb0e2d659a7c8d615a8680de Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 14 Sep 2022 19:19:54 +0800 Subject: [PATCH 31/36] Deleted editor folder --- .../Editor/Backend/SHImGuiVulkanBackend.cpp | 396 ------------------ .../src/Editor/Backend/SHImGuiVulkanBackend.h | 86 ---- SHADE_Engine/src/Editor/SHEditor.cpp | 127 ------ SHADE_Engine/src/Editor/SHEditor.h | 31 -- SHADE_Engine/src/Editor/SHEditorBackend.h | 3 - 5 files changed, 643 deletions(-) delete mode 100644 SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp delete mode 100644 SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h delete mode 100644 SHADE_Engine/src/Editor/SHEditor.cpp delete mode 100644 SHADE_Engine/src/Editor/SHEditor.h delete mode 100644 SHADE_Engine/src/Editor/SHEditorBackend.h diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp deleted file mode 100644 index 676625bb..00000000 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.cpp +++ /dev/null @@ -1,396 +0,0 @@ - -#include -#include - -#include - -#include "SHImGuiVulkanBackend.h" - -#include - - -#include "Tools/SHLogger.h" -namespace SHADE -{ -#define GETINSTANCE \ - ImGuiIO& io = ImGui::GetIO(); \ - SHBreachInstance& instance = *reinterpret_cast(io.UserData); - - /* - #==============================================================# - || Embedded Shaders || - #==============================================================# - */ - // glsl_shader.vert, compiled with: - // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert - /* - #version 450 core - layout(location = 0) in vec2 aPos; - layout(location = 1) in vec2 aUV; - layout(location = 2) in vec4 aColor; - layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc; - - out gl_PerVertex { vec4 gl_Position; }; - layout(location = 0) out struct { vec4 Color; vec2 UV; } Out; - - void main() - { - Out.Color = aColor; - Out.UV = aUV; - gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); - } - */ - static std::vector __glsl_shader_vert_spv = - { - 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b, - 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, - 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015, - 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, - 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43, - 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f, - 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005, - 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000, - 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c, - 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074, - 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001, - 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b, - 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015, - 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047, - 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e, - 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008, - 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002, - 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017, - 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020, - 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015, - 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020, - 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020, - 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020, - 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020, - 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a, - 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014, - 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f, - 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021, - 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006, - 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8, - 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012, - 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016, - 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018, - 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022, - 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008, - 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013, - 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024, - 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006, - 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b, - 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e, - 0x0000002d,0x0000002c,0x000100fd,0x00010038 - }; - - // glsl_shader.frag, compiled with: - // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag - /* - #version 450 core - layout(location = 0) out vec4 fColor; - layout(set=0, binding=0) uniform sampler2D sTexture; - layout(location = 0) in struct { vec4 Color; vec2 UV; } In; - void main() - { - fColor = In.Color * texture(sTexture, In.UV.st); - } - */ - static std::vector __glsl_shader_frag_spv = - { - 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b, - 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, - 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010, - 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, - 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000, - 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001, - 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574, - 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e, - 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021, - 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006, - 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003, - 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006, - 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001, - 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020, - 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001, - 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000, - 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000, - 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018, - 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004, - 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d, - 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017, - 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a, - 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085, - 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd, - 0x00010038 - }; - - /* - #==============================================================# - || ImGui Push Constants || - #==============================================================# - */ - struct ImGui_Push_Constants - { - std::array scale; - std::array translate; - }; - - void SHImGuiVulkanBackend::CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex, SHWindow& mainWindow) noexcept - { - if (ImGui::GetCurrentContext() == nullptr) - { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - } - else { SHLOG_WARNING("ImGui context already exists") }; - - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. - //io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) - //io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) - io.BackendRendererName = "SHImGuiVulkanBackend"; - - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - ImGuiPlatformIO& platformIO = ImGui::GetPlatformIO(); - platformIO.Renderer_CreateWindow = CreateChildWindow; - platformIO.Renderer_DestroyWindow = DestroyChildWindow; - platformIO.Renderer_SetWindowSize = SetChildWindowSize; - platformIO.Renderer_RenderWindow = RenderChildWindow; - platformIO.Renderer_SwapBuffers = ChildSwapBuffers; - - platformIO.Platform_CreateWindow = CreateChildWindow; - platformIO.Platform_DestroyWindow = DestroyChildWindow; - platformIO.Platform_ShowWindow = [](ImGuiViewport* pViewport) {}; - platformIO.Platform_SetWindowPos = SetChildWindowPos; - platformIO.Platform_GetWindowPos = GetChildWindowPos; - platformIO.Platform_SetWindowSize = SetChildWindowSize; - platformIO.Platform_GetWindowSize = GetChildWindowSize; - //platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus; - //platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus; - //platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized; - platformIO.Platform_SetWindowTitle = [](ImGuiViewport* pViewport, const char*) {}; - platformIO.Platform_RenderWindow = RenderChildWindow; - platformIO.Platform_SwapBuffers = ChildSwapBuffers; - - platformIO.Monitors.resize(0); - ImGuiPlatformMonitor monitor; - monitor.MainPos = monitor.WorkPos = {}; - monitor.MainSize = monitor.WorkSize = {}; - platformIO.Monitors.push_back(monitor); - }//if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - - //associate io keymap with engine input keymap - - //Initialize instance - auto instance = std::make_unique(); - instance->window = mainWindow; - instance->InitializePipeline(io, renderpass, subpassIndex); - instance->lastFrameTime = std::chrono::high_resolution_clock::now(); - io.UserData = instance.release(); - - ImGuiViewport* viewport = ImGui::GetMainViewport(); - viewport->RendererUserData = io.UserData; - viewport->PlatformHandle = io.UserData; - } - - void SHImGuiVulkanBackend::Render(Handle const& commandBuffer) noexcept - { - GETINSTANCE - ImGui::Render(); - ImDrawData* drawData = ImGui::GetDrawData(); - //instance.Render(io, drawData, ) - } - - void SHImGuiVulkanBackend::CreateChildWindow(ImGuiViewport* viewport) noexcept - { - GETINSTANCE - - auto& info = *new SHImGuiWindow{ instance.device }; - info.window.Create({}, instance.window.GetHWND()); - viewport->RendererUserData = &info; - } - - void SHImGuiVulkanBackend::DestroyChildWindow(ImGuiViewport* viewport) noexcept - { - GETINSTANCE - - auto info = reinterpret_cast(viewport->RendererUserData); - info->window.Close(); - info->window.Destroy(); - delete info; - viewport->RendererUserData = nullptr; - } - - ImVec2 SHImGuiVulkanBackend::GetChildWindowSize(ImGuiViewport* viewport) noexcept - { - auto info = reinterpret_cast(viewport->RendererUserData); - return{}; - } - - void SHImGuiVulkanBackend::SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept - { - auto& info = *reinterpret_cast(viewport->RendererUserData); - SetWindowPos - ( - info.window.GetHWND(), - HWND_TOPMOST, - -1, - -1, - static_cast(size.x), - static_cast(size.y), - SWP_NOMOVE | SWP_NOZORDER - ); - } - - ImVec2 SHImGuiVulkanBackend::GetChildWindowPos(ImGuiViewport* viewport) noexcept - { - auto& info = *reinterpret_cast(viewport->RendererUserData); - auto [x, y] = info.window.GetWindowSize(); - return {static_cast(x), static_cast(y)}; - } - - void SHImGuiVulkanBackend::SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept - { - auto& info = *reinterpret_cast(viewport->RendererUserData); - info.window.SetPosition(size.x, size.y); - } - - void SHImGuiVulkanBackend::RenderChildWindow(ImGuiViewport* viewport, void*) noexcept - { - GETINSTANCE; - auto& info = *reinterpret_cast(viewport->RendererUserData); - //info.Render(io, viewport->DrawData, instance);; - } - - void SHImGuiVulkanBackend::ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept - { - } - - - void SHImGuiVulkanBackend::SHImGuiWindow::InitializeBuffers(void) noexcept - { - for (auto& primBuffer : primitiveBuffers) - { - primBuffer.vertexBuffer = device->CreateBuffer(sizeof(ImDrawVert) * 3000, - nullptr, - sizeof(ImDrawVert) * 3000, - vk::BufferUsageFlagBits::eVertexBuffer, - VMA_MEMORY_USAGE_AUTO, - VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags - - primBuffer.indicesBuffer = device->CreateBuffer(sizeof(ImDrawIdx) * 3000, - nullptr, - sizeof(ImDrawIdx) * 3000, - vk::BufferUsageFlagBits::eIndexBuffer, - VMA_MEMORY_USAGE_AUTO, - VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // alloc flags - - } - } - - void SHImGuiVulkanBackend::SHImGuiWindow::Render(ImGuiIO& io, ImDrawData* drawData, - Handle descriptorSetGroup) - { - - } - - - void SHImGuiVulkanBackend::SHBreachInstance::CreateFontsTexture(Handle image, ImGuiIO& io) noexcept - { - // Build texture atlas - unsigned char* pixels; - int width, height; - - // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. - // If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. - io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); - - SHImageCreateParams createParams{}; - createParams.imageType = vk::ImageType::e2D; - createParams.width = width; - createParams.height = height; - createParams.depth = 1; - createParams.levels = 1; - createParams.arrayLayers = 1; - createParams.imageFormat = vk::Format::eR8G8B8A8Unorm; - createParams.usageFlags = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst; - createParams.createFlags = {}; - - fontsMipOffset.push_back(0); - - // Create image. Data should be in staging only now - fontsTexture = device->CreateImage(createParams, pixels, width * height * sizeof(uint32_t), fontsMipOffset, VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, {}); - - // TODO: Copy the image to device memory here - // 1. Start record - // 2. Do transfer - // 3. End record - // 4. execute cmd buffer - - // We could put here the texture id if we wanted - io.Fonts->TexID = nullptr; - } - - void SHImGuiVulkanBackend::SHBreachInstance::InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept - { - // Vertex input state - SHVertexInputState vInputState{}; - vInputState.AddBinding(false, true, - { - SHVertexAttribute(SHAttribFormat::FLOAT_2D), - SHVertexAttribute(SHAttribFormat::FLOAT_2D), - SHVertexAttribute(SHAttribFormat::UINT32_1D), - } - ); - - // Shaders - Handle vs = device->CreateShaderModule(__glsl_shader_vert_spv, "main", vk::ShaderStageFlagBits::eVertex, "__glsl_shader_vert_spv"); - Handle fs = device->CreateShaderModule(__glsl_shader_frag_spv, "main", vk::ShaderStageFlagBits::eFragment, "__glsl_shader_frag_spv"); - - // pipeline layout initialize shaders - SHPipelineLayoutParams pipelineLayoutParams - { - .shaderModules = std::move (std::vector> - { - vs, - fs, - }), - }; - - // Create pipeline layout - Handle pipelineLayouot = device->CreatePipelineLayout(pipelineLayoutParams); - - // Create pipeline (but dont construct it yet) - Handle newPipeline = device->CreatePipeline(pipelineLayouot, nullptr, renderpass, subpassIndex, SH_PIPELINE_TYPE::GRAPHICS); - - // Set the vertex input state - newPipeline->GetPipelineState().SetVertexInputState(vInputState); - - // Actually construct the pipeline - newPipeline->ConstructPipeline(); - - SHVkDescriptorPool::Config config - { - .MaxSets = 10, - .Flags = {}, - }; - - // Create a descriptor pool - descriptorPool = device->CreateDescriptorPools(config); - - // Allocate descriptor sets required for the fonts - descriptorSetGroup = - } - - void SHImGuiVulkanBackend::SHBreachInstance::Render(Handle const& commandBuffer, ImGuiIO& io, ImDrawData* draw_data, Handle const& descriptorSetGroup) noexcept - { - - } - -} \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h b/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h deleted file mode 100644 index 47f31e4c..00000000 --- a/SHADE_Engine/src/Editor/Backend/SHImGuiVulkanBackend.h +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -#include -#include "Graphics/Instance/SHVkInstance.h" -#include "Graphics/Buffers/SHVkBuffer.h" -#include "Graphics/Images/SHVkImage.h" -#include "Graphics/Devices/SHVkPhysicalDevice.h" -#include "Graphics/Devices/SHVkLogicalDevice.h" -#include "Graphics/Pipeline/SHVkPipeline.h" -#include "Graphics/Swapchain/SHVkSwapchain.h" -#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" -#include "Graphics/Debugging/SHVulkanDebugUtil.h" -#include "Graphics/Windowing/SHWindow.h" - -namespace SHADE -{ - class SHImGuiVulkanBackend - { - struct SHImGuiWindow - { - struct Buffers - { - Handle vertexBuffer; - Handle indicesBuffer; - }; - //Required to create stuff - Handle device; - - // Window of the imgui window - SHWindow window; - - // Buffers required to draw the imgui entities - std::array primitiveBuffers; - - public: - void InitializeBuffers(void) noexcept; - void Render(ImGuiIO& io, ImDrawData* drawData, Handle descriptorSetGroup); - }; - - struct SHBreachInstance : public SHImGuiWindow - { - // pipeline/shaders used to render imgui entities - Handle pipeline; - - // Last frame time - std::chrono::time_point lastFrameTime; - - // Fonts texture - Handle fontsTexture; - - // mip maps for font image (should just contain 1) - std::vector fontsMipOffset; - - // Descriptor pool rquired to allocate descriptor sets - Handle descriptorPool; - - // Descriptor set required to store font texture - Handle descriptorSetGroup; - - public: - void CreateFontsTexture (Handle image, ImGuiIO& io) noexcept; - void InitializePipeline(ImGuiIO& io, Handle const& renderpass, uint32_t subpassIndex) noexcept; - void Render (Handle const& commandBuffer, ImGuiIO& io, ImDrawData* draw_data, Handle const& descriptorSetGroup) noexcept; - - }; - public: - static void CreateInstance(Handle const& logicalDevice, Handle const& renderpass, uint32_t subpassIndex, SHWindow& mainWindow) noexcept; - static void Render(Handle const& commandBuffer) noexcept; - - - static void EnableDocking(void) noexcept; - - //Platform specific functions - static void CreateChildWindow(ImGuiViewport* viewport) noexcept; - static void DestroyChildWindow(ImGuiViewport* viewport) noexcept; - static ImVec2 GetChildWindowSize(ImGuiViewport* viewport) noexcept; - static void SetChildWindowSize(ImGuiViewport* viewport, ImVec2 size) noexcept; - static ImVec2 GetChildWindowPos(ImGuiViewport* viewport) noexcept; - static void SetChildWindowPos(ImGuiViewport* viewport, ImVec2 size) noexcept; - static void RenderChildWindow(ImGuiViewport* viewport, void*) noexcept; - static void ChildSwapBuffers(ImGuiViewport* viewport, void*) noexcept; - - private: - - }; -} \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp deleted file mode 100644 index ab3a220a..00000000 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include "SHpch.h" -#include "SHEditor.h" -#include "SHEditorBackend.h" - -#include - -#include -#include -#include -#include -#include "Graphics/Swapchain/SHVkSwapchain.h" -#include "Graphics/Debugging/SHVulkanDebugUtil.h" - -#include "Tools/SHLogger.h" - -extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); - -namespace SHADE -{ - Handle SHEditor::cmdPool; - Handle SHEditor::cmdBuffer; - - void SHEditor::Initialize(HWND hwnd) - { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - - ImGuiIO& io = ImGui::GetIO(); - - //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - - SetupWin32Backend(hwnd); - SetupVulkanBackend(); - - - } - - void SHEditor::PreRender() - { - cmdPool->Reset(); - } - - void SHEditor::Update() - { - NewFrame(); - - //Add all windows to draw list, Perform necessary updates - //ImGui::ShowDemoWindow(); - - ImGui::Render(); - } - - void SHEditor::Render() - { - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmdBuffer->GetVkCommandBuffer()); - - ImGuiIO& io = ImGui::GetIO(); - if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - } - - } - - void SHEditor::Exit() - { - ShutdownVulkanBackend(); - ShutdownWin32Backend(); - ImGui::DestroyContext(); - } - - void SHEditor::SetupVulkanBackend() - { - ImGui_ImplVulkan_InitInfo initInfo; - - initInfo.Instance = SHVkInstance::GetVkInstance(); - auto gfxSystem = reinterpret_cast(SHSystemManager::GetSystem("Graphics System")); - initInfo.PhysicalDevice = gfxSystem->GetPhysicalDevice()->GetVkPhysicalDevice(); - initInfo.Device = gfxSystem->GetDevice()->GetVkLogicalDevice(); - initInfo.Queue = gfxSystem->GetQueue()->GetVkQueue(); - initInfo.MinImageCount = initInfo.ImageCount = gfxSystem->GetSwapchain()->GetNumImages(); - initInfo.DescriptorPool = gfxSystem->GetDescriptorPool()->GetVkHandle(); - initInfo.MSAASamples = VK_SAMPLE_COUNT_1_BIT; - initInfo.Allocator = nullptr; - initInfo.PipelineCache = nullptr; - initInfo.Subpass = 0; - - initInfo.CheckVkResultFn = [](VkResult err) - { - if (err == VK_SUCCESS) - return; - SHVulkanDebugUtil::ReportVkError(vk::Result(err), "Editor Error"); - }; - - ImGui_ImplVulkan_Init(&initInfo, gfxSystem->GetRenderGraph().GetNode("G-Buffer")->GetRenderpass()->GetVkRenderpass()); - - cmdPool = gfxSystem->GetDevice()->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); - cmdBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); - - ImGui_ImplVulkan_CreateFontsTexture(cmdBuffer->GetVkCommandBuffer()); - } - - void SHEditor::ShutdownVulkanBackend() - { - ImGui_ImplVulkan_Shutdown(); - } - - void SHEditor::NewFrame() - { - ImGui_ImplVulkan_NewFrame(); - ImGui_ImplWin32_NewFrame(); - ImGui::NewFrame(); - } - - void SHEditor::SetupWin32Backend(HWND hwnd) - { - ImGui_ImplWin32_Init(hwnd); - } - - void SHEditor::ShutdownWin32Backend() - { - } - -} diff --git a/SHADE_Engine/src/Editor/SHEditor.h b/SHADE_Engine/src/Editor/SHEditor.h deleted file mode 100644 index be83e22b..00000000 --- a/SHADE_Engine/src/Editor/SHEditor.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "Graphics/Commands/SHVkCommandPool.h" - -namespace SHADE -{ - class SHEditor - { - public: - - static void Initialize(HWND hwnd); - - static void PreRender(); - - static void Update(); - - static void Render(); - - static void Exit(); - - private: - static void SetupWin32Backend(HWND hwnd); - static void ShutdownWin32Backend(); - static void SetupVulkanBackend(); - static void ShutdownVulkanBackend(); - - static void NewFrame(); - - static Handle cmdPool; - static Handle cmdBuffer; - }; -} diff --git a/SHADE_Engine/src/Editor/SHEditorBackend.h b/SHADE_Engine/src/Editor/SHEditorBackend.h deleted file mode 100644 index 29a19f86..00000000 --- a/SHADE_Engine/src/Editor/SHEditorBackend.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once -#include -#include \ No newline at end of file From 169822c2216efcd89ad27d72df8ddc9a06828338 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 15 Sep 2022 09:16:13 +0800 Subject: [PATCH 32/36] Render graph execution fixed GetVkQueue function deleted. More specific function implemented (Present). --- .../src/Application/SBApplication.cpp | 1 + .../Graphics/Commands/SHVkCommandBuffer.cpp | 22 +++++- .../src/Graphics/Commands/SHVkCommandBuffer.h | 2 + .../src/Graphics/Commands/SHVkCommandPool.cpp | 15 ++-- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 26 ++++--- .../MiddleEnd/Interface/SHGraphicsSystem.h | 4 +- .../src/Graphics/Queues/SHVkQueue.cpp | 73 +++++++++++++++---- SHADE_Engine/src/Graphics/Queues/SHVkQueue.h | 5 +- .../Graphics/RenderGraph/SHRenderGraph.cpp | 23 ++++-- .../src/Graphics/RenderGraph/SHRenderGraph.h | 3 +- 10 files changed, 127 insertions(+), 47 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index a30237dc..c47b8016 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -48,6 +48,7 @@ namespace Sandbox //SHADE::SHEditor::PreRender(); //#endif graphicsSystem->BeginRender(); + graphicsSystem->Run(1.0f); //#ifdef SHEDITOR //SHADE::SHEditor::PreRender(); //SHADE::SHEditor::Update(); diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index 5b2d15fe..f83087dd 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -76,7 +76,10 @@ namespace SHADE // Check if command buffer is ready to record. if (cmdBufferState != SH_CMD_BUFFER_STATE::INITIAL) { - SHLOG_ERROR("Command buffer not in initial state, cannot begin recording. "); + SHLOG_ERROR("Command buffer not in initial state, cannot begin recording. Command buffer could be: \n" + "- corrupted and in invalid state\n" + "- in executable state\n" + "- in pending state\n"); return; } @@ -182,7 +185,9 @@ namespace SHADE // Check if render area is optimal if (!IsRenderAreaOptimal(renderpassHdl, framebufferExtent, renderPassInfo.renderArea)) + { SHLOG_ERROR("Render area in renderpass begin info is not optimal. See Vulkan vkGetRenderAreaGranularity for details."); + } // Begin the render pass vkCommandBuffer.beginRenderPass (&renderPassInfo, vk::SubpassContents::eInline); @@ -431,6 +436,16 @@ namespace SHADE ); } + bool SHVkCommandBuffer::IsReadyToSubmit(void) const noexcept + { + return cmdBufferState == SH_CMD_BUFFER_STATE::EXECUTABLE; + } + + void SHVkCommandBuffer::HandlePostSubmit(void) noexcept + { + SetState(SH_CMD_BUFFER_STATE::PENDING); + } + //void SHVkCommandBuffer::PipelineBarrier(vk::PipelineStageFlags ) const noexcept //{ // //vkCommandBuffer.pipelineBarrier() @@ -493,9 +508,7 @@ namespace SHADE { vk::Extent2D granularity = parentPool->GetLogicalDevice()->GetVkLogicalDevice().getRenderAreaGranularity(renderpassHdl->GetVkRenderpass()); - return (renderArea.offset.x % granularity.width == 0 && renderArea.offset.y % granularity.height == 0 && - (renderArea.extent.width % granularity.width || renderArea.offset.x + renderArea.extent.width == framebufferExtent.width) && - (renderArea.extent.height % granularity.height || renderArea.offset.y + renderArea.extent.height == framebufferExtent.height)); + return (renderArea.offset.x % granularity.width == 0 && renderArea.offset.y % granularity.height == 0 && (renderArea.extent.width % granularity.width || renderArea.offset.x + renderArea.extent.width == framebufferExtent.width) && (renderArea.extent.height % granularity.height || renderArea.offset.y + renderArea.extent.height == framebufferExtent.height)); } /***************************************************************************/ @@ -559,6 +572,7 @@ namespace SHADE , usageFlags{} , commandBufferCount{ 0 } , parentPool{commandPool} + , pushConstantData{} { vk::CommandBufferAllocateInfo allocateInfo{}; diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index d2171c25..948092bf 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -125,6 +125,8 @@ namespace SHADE std::vector const& imageMemoryBarriers ) const noexcept; + bool IsReadyToSubmit (void) const noexcept; + void HandlePostSubmit (void) noexcept; // Push Constant variable setting template diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandPool.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandPool.cpp index 5cf4bea4..881ee998 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandPool.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandPool.cpp @@ -150,20 +150,19 @@ namespace SHADE logicalDeviceHdl->GetVkLogicalDevice().resetCommandPool(vkCommandPool, vk::CommandPoolResetFlagBits::eReleaseResources); for (auto& primary : primaries) { - if (primary->GetState() != SH_CMD_BUFFER_STATE::PENDING) - primary->SetState(SH_CMD_BUFFER_STATE::INITIAL); - else - SHLOG_ERROR("Primary command buffer in pending state, could not reset. "); + // #NoteToSelf: Since there is no way to set the state of a command buffer back to initial, we just hard set it to initial. Ditto for secondaries. + //if (primary->GetState() != SH_CMD_BUFFER_STATE::PENDING) + primary->SetState(SH_CMD_BUFFER_STATE::INITIAL); + + // From the spec: Any primary command buffer allocated from another VkCommandPool that is in the recording or // executable state and has a secondary command buffer allocated from commandPool recorded into it, // becomes invalid. TODO: Might want to check and throw exception for these conditions after making sure this actually happens using validation layers. } for (auto& secondary : secondaries) { - if (secondary->GetState() != SH_CMD_BUFFER_STATE::PENDING) - secondary->SetState(SH_CMD_BUFFER_STATE::INITIAL); - else - SHLOG_ERROR("Secondary command buffer in pending state, could not reset. "); + //if (secondary->GetState() != SH_CMD_BUFFER_STATE::PENDING) + secondary->SetState(SH_CMD_BUFFER_STATE::INITIAL); // TODO: Ditto from TODO in primary check } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 4d4ca4f4..63fd7466 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -54,7 +54,7 @@ namespace SHADE }); // Create graphics queue - queue = device->GetQueue(SH_Q_FAM::GRAPHICS, 0); + graphicsQueue = device->GetQueue(SH_Q_FAM::GRAPHICS, 0); // Create Render Context @@ -101,7 +101,7 @@ namespace SHADE auto node = renderGraph.AddNode("G-Buffer", { "Position", "Normals", "Composite" }, {}); // no predecessors // First subpass to write to G-Buffer - auto writeSubpass = node->AddSubpass("G-Buffer Write"); + auto writeSubpass = node->AddSubpass("G-Buffer Write"); writeSubpass->AddColorOutput("Position"); writeSubpass->AddColorOutput("Normals"); @@ -111,11 +111,11 @@ namespace SHADE compositeSubpass->AddInput("Normals"); compositeSubpass->AddInput("Position"); - auto compositeNode = renderGraph.AddNode("Bloom", { "Composite", "Downscale", "Present"}, {"G-Buffer"}); - auto bloomSubpass = compositeNode->AddSubpass("Downsample"); - bloomSubpass->AddInput("Composite"); - bloomSubpass->AddColorOutput("Downscale"); - bloomSubpass->AddColorOutput("Present"); + //auto compositeNode = renderGraph.AddNode("Bloom", { "Composite", "Downscale", "Present"}, {"G-Buffer"}); + //auto bloomSubpass = compositeNode->AddSubpass("Downsample"); + //bloomSubpass->AddInput("Composite"); + // bloomSubpass->AddColorOutput("Downscale"); + // bloomSubpass->AddColorOutput("Present"); renderGraph.Generate(); @@ -127,6 +127,14 @@ namespace SHADE void SHGraphicsSystem::Run(float dt) { + auto const& frameData = renderContext.GetCurrentFrameData(); + + renderGraph.Execute(renderContext.GetCurrentFrame()); + + graphicsQueue->SubmitCommandBuffer({ renderGraph.GetCommandBuffer(renderContext.GetCurrentFrame()) }, + { frameData.semRenderFinishHdl }, + { frameData.semImgAvailableHdl }, + vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests | vk::PipelineStageFlagBits::eLateFragmentTests); } @@ -134,7 +142,7 @@ namespace SHADE { //renderPass.Free(); renderContext.Destroy(); - queue.Free(); + graphicsQueue.Free(); swapchain.Free(); surface.Free(); device.Free(); @@ -208,7 +216,7 @@ namespace SHADE presentInfo.pImageIndices = &CURR_FRAME_IDX; // #BackEndTest: queues an image for presentation - if (auto result = device->GetQueue(SH_Q_FAM::GRAPHICS, 0)->GetVkQueue().presentKHR(&presentInfo); result != vk::Result::eSuccess) + if (auto result = graphicsQueue->Present(swapchain, {currFrameData.semRenderFinishHdl}, CURR_FRAME_IDX); result != vk::Result::eSuccess) { // If swapchain is incompatible/outdated if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 27d03fd3..8c803e1d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -114,7 +114,7 @@ namespace SHADE Handle GetSwapchain() const { return swapchain; } Handle GetSurface() const { return surface; } Handle GetPhysicalDevice() const {return physicalDevice;} - Handle GetQueue() const { return queue; } + Handle GetQueue() const { return graphicsQueue; } Handle GetDescriptorPool() const { return descPool; } SHRenderGraph const& GetRenderGraph (void) const noexcept; @@ -130,7 +130,7 @@ namespace SHADE Handle device; Handle surface; Handle swapchain; - Handle queue; + Handle graphicsQueue; Handle descPool; //Handle renderPass; // Potentially bring out? std::vector screenSegments; diff --git a/SHADE_Engine/src/Graphics/Queues/SHVkQueue.cpp b/SHADE_Engine/src/Graphics/Queues/SHVkQueue.cpp index 9f8c529c..6ed6def6 100644 --- a/SHADE_Engine/src/Graphics/Queues/SHVkQueue.cpp +++ b/SHADE_Engine/src/Graphics/Queues/SHVkQueue.cpp @@ -5,6 +5,7 @@ #include "Graphics/Synchronization/SHVkSemaphore.h" #include "Graphics/Synchronization/SHVkFence.h" #include "Graphics/Commands/SHVkCommandBuffer.h" +#include "Graphics/Swapchain/SHVkSwapchain.h" namespace SHADE @@ -31,14 +32,10 @@ namespace SHADE } - vk::Queue SHVkQueue::GetVkQueue(void) const noexcept + void SHVkQueue::SubmitCommandBuffer(std::initializer_list> cmdBuffers, std::initializer_list> signalSems /*= {}*/, std::initializer_list> waitSems /*= {}*/, vk::PipelineStageFlags waitDstStageMask /*= {}*/, Handle const& optionalFence /*= {}*/) noexcept { - return vkQueue; - } - - void SHVkQueue::SubmitCommandBuffer(std::initializer_list> cmdBuffers, std::initializer_list> signalSems /*= {}*/, std::initializer_list> waitSems /*= {}*/, vk::PipelineStageFlagBits waitDstStageMask /*= {}*/, Handle const& optionalFence /*= {}*/) noexcept - { - std::vector vkWaitSems{ waitSems.size() }; + // prepare wait sems + std::array vkWaitSems{ }; uint32_t i = 0; for (auto& sem : waitSems) { @@ -46,7 +43,8 @@ namespace SHADE ++i; } - std::vector vkSignalSems{ signalSems.size() }; + // prepare signal sems + std::array vkSignalSems{ }; i = 0; for (auto& sem : signalSems) { @@ -54,33 +52,76 @@ namespace SHADE ++i; } - std::vector vkCmdBuffers{ cmdBuffers.size() }; + // prepare cmd buffers + std::array vkCmdBuffers{ }; i = 0; for (auto& cmdBuffer : cmdBuffers) { + // Check if command buffer is in executable state + if (!cmdBuffer->IsReadyToSubmit()) + { + SHLOG_ERROR("Command buffer is not in executable state. Cannot submit command buffer. "); + return; + } + vkCmdBuffers[i] = cmdBuffer->GetVkCommandBuffer(); ++i; } - vk::PipelineStageFlags mask = waitDstStageMask; - + // Prepare submit info vk::SubmitInfo submitInfo { - .waitSemaphoreCount = static_cast(vkWaitSems.size()), + .waitSemaphoreCount = static_cast(waitSems.size()), .pWaitSemaphores = vkWaitSems.data(), - .pWaitDstStageMask = &mask, - .commandBufferCount = static_cast(vkCmdBuffers.size()), + .pWaitDstStageMask = &waitDstStageMask, + .commandBufferCount = static_cast(cmdBuffers.size()), .pCommandBuffers = vkCmdBuffers.data(), - .signalSemaphoreCount = static_cast(vkSignalSems.size()), + .signalSemaphoreCount = static_cast(signalSems.size()), .pSignalSemaphores = vkSignalSems.data(), }; - // #BackEndTest: Submit the queue + // Submit the queue if (auto result = vkQueue.submit(1, &submitInfo, (optionalFence) ? optionalFence->GetVkFence() : nullptr); result != vk::Result::eSuccess) { SHVulkanDebugUtil::ReportVkError(result, "Failed to submit command buffer. "); } + else // if success + { + // Change all command buffers to pending state + for (Handle cmdBuffer : cmdBuffers) + { + cmdBuffer->HandlePostSubmit(); + } + } + } + vk::Result SHVkQueue::Present(Handle const& swapchain, std::initializer_list> waitSems, uint32_t frameIndex) noexcept + { + vk::PresentInfoKHR presentInfo{}; + + // prepare wait sems + std::array vkWaitSems{ }; + uint32_t i = 0; + for (auto& sem : waitSems) + { + vkWaitSems[i] = sem->GetVkSem(); + ++i; + } + + // prepare presentation information + presentInfo.waitSemaphoreCount = static_cast(waitSems.size()); + presentInfo.pWaitSemaphores = vkWaitSems.data(); + presentInfo.swapchainCount = 1; + presentInfo.pSwapchains = &swapchain->GetVkSwapchain(); + presentInfo.pImageIndices = &frameIndex; + + // Present swapchain image. + auto result = vkQueue.presentKHR(&presentInfo); + if (result != vk::Result::eSuccess) + { + SHLOGV_ERROR ("Failed to present swapchain image. "); + } + return result; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Queues/SHVkQueue.h b/SHADE_Engine/src/Graphics/Queues/SHVkQueue.h index 203ae59e..042bc4bb 100644 --- a/SHADE_Engine/src/Graphics/Queues/SHVkQueue.h +++ b/SHADE_Engine/src/Graphics/Queues/SHVkQueue.h @@ -11,6 +11,7 @@ namespace SHADE class SHVkCommandBuffer; class SHVkFence; class SHVkSemaphore; + class SHVkSwapchain; enum class SH_QUEUE_FAMILY_ARRAY_INDEX : std::size_t { @@ -45,9 +46,9 @@ namespace SHADE public: SHVkQueue(vk::Queue inVkQueue, SHQueueFamilyIndex parent, Handle const& inLogicalDeviceHdl) noexcept; - vk::Queue GetVkQueue(void) const noexcept; - void SubmitCommandBuffer(std::initializer_list> cmdBuffers, std::initializer_list> signalSems = {}, std::initializer_list> waitSems = {}, vk::PipelineStageFlagBits waitDstStageMask = {}, Handle const& fence = {}) noexcept; + void SubmitCommandBuffer (std::initializer_list> cmdBuffers, std::initializer_list> signalSems = {}, std::initializer_list> waitSems = {}, vk::PipelineStageFlags waitDstStageMask = {}, Handle const& fence = {}) noexcept; + vk::Result Present (Handle const& swapchain, std::initializer_list> waitSems, uint32_t frameIndex) noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index 71bac1e6..76106d67 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -552,14 +552,16 @@ namespace SHADE void SHRenderGraphNode::Execute(Handle& commandBuffer, uint32_t frameIndex) noexcept { + frameIndex = (framebuffers.size() > 1) ? frameIndex : 0; commandBuffer->BeginRenderpass(renderpass, framebuffers[frameIndex]); - for (auto& subpass : subpasses) + for (uint32_t i = 0; i < subpasses.size(); ++i) { - subpass->Execute(commandBuffer); + subpasses[i]->Execute(commandBuffer); - // Go to next subpass - commandBuffer->NextSubpass(); + // Go to next subpass if not last subpass + if (i != subpasses.size() - 1) + commandBuffer->NextSubpass(); } commandBuffer->EndRenderpass(); @@ -892,6 +894,10 @@ namespace SHADE { commandPool = logicalDeviceHdl->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); commandBuffers.resize(static_cast(swapchainHdl->GetNumImages())); + for (auto& cmdBuffer : commandBuffers) + { + cmdBuffer = commandPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + } } /***************************************************************************/ @@ -1019,6 +1025,8 @@ namespace SHADE void SHRenderGraph::Execute(uint32_t frameIndex) noexcept { + commandPool->Reset(); + auto& cmdBuffer = commandBuffers[frameIndex]; cmdBuffer->BeginRecording(); @@ -1027,7 +1035,7 @@ namespace SHADE node->Execute(commandBuffers[frameIndex], frameIndex); } - cmdBuffer->EndRenderpass(); + cmdBuffer->EndRecording(); } Handle SHRenderGraph::GetNode(std::string const& nodeName) const noexcept @@ -1038,4 +1046,9 @@ namespace SHADE return {}; } + Handle const& SHRenderGraph::GetCommandBuffer(uint32_t frameIndex) const noexcept + { + return commandBuffers[frameIndex]; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index ea913baa..91f3f2ca 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -275,7 +275,8 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ - Handle GetNode (std::string const& nodeName) const noexcept; + Handle GetNode (std::string const& nodeName) const noexcept; + Handle const& GetCommandBuffer (uint32_t frameIndex) const noexcept; }; } From a90c13744543c80005dada71c61ab7877938816e Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 15 Sep 2022 12:10:06 +0800 Subject: [PATCH 33/36] #comment Completed System Routine Added Unit Test version 1 --- .../src/Application/SBApplication.cpp | 5 +- SHADE_Engine/SHADE_Engine.vcxproj | 4 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 4 + .../src/ECS_Base/Managers/SHSystemManager.cpp | 8 +- .../src/ECS_Base/Managers/SHSystemManager.h | 9 +- .../ECS_Base/System/SHFixedSystemRoutine.cpp | 3 +- .../src/ECS_Base/System/SHRoutineStats.h | 11 +- SHADE_Engine/src/ECS_Base/System/SHSystem.h | 39 ++-- .../src/ECS_Base/System/SHSystemRoutine.cpp | 2 + .../ECS_Base/UnitTesting/SHECSUnitTest.cpp | 181 ++++++++++++++++++ .../src/ECS_Base/UnitTesting/SHECSUnitTest.h | 22 +++ .../ECS_Base/UnitTesting/SHTestComponents.h | 30 +++ .../src/ECS_Base/UnitTesting/SHTestSystems.h | 40 ++++ 13 files changed, 318 insertions(+), 40 deletions(-) create mode 100644 SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.cpp create mode 100644 SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.h create mode 100644 SHADE_Engine/src/ECS_Base/UnitTesting/SHTestComponents.h create mode 100644 SHADE_Engine/src/ECS_Base/UnitTesting/SHTestSystems.h diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 184b9611..3577475d 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -1,5 +1,6 @@ #include "SBpch.h" #include "SBApplication.h" +#include "ECS_Base/UnitTesting/SHECSUnitTest.h" #ifdef SHEDITOR #include "Editor/SHEditor.h" @@ -23,11 +24,13 @@ namespace Sandbox { window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow); - + #ifdef SHEDITOR #else #endif + + } void SBApplication::Update(void) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index d84f3c05..2b2541bb 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -118,6 +118,9 @@ + + + @@ -204,6 +207,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 8fc412c5..4f80a4f8 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -388,6 +388,9 @@ + + + @@ -591,5 +594,6 @@ + \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp index acf7cba3..551233db 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.cpp @@ -34,7 +34,7 @@ namespace SHADE } } - void SHSystemManager::RunRoutines(bool editorPause) noexcept + void SHSystemManager::RunRoutines(bool editorPause, double deltaTime) noexcept { for (auto& routine : systemRoutineContainer) { @@ -43,7 +43,7 @@ namespace SHADE if (routine.get()->IsRunInEditorPause) { std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); - routine.get()->Execute(0.0); + routine.get()->Execute(deltaTime); std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); routine.get()->stats.executionTime = std::chrono::duration(end - start).count(); } @@ -51,7 +51,7 @@ namespace SHADE else { std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); - routine.get()->Execute(0.0); + routine.get()->Execute(deltaTime); std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); routine.get()->stats.executionTime = std::chrono::duration(end - start).count(); } @@ -61,6 +61,8 @@ namespace SHADE void SHSystemManager::Exit() noexcept { + systemRoutineContainer.clear(); + for (SystemContainer::reverse_iterator it = systemContainer.rbegin(); it != systemContainer.rend(); ++it) { (*it).second->Exit(); diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h index 616a8db3..f97d98a2 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHSystemManager.h @@ -55,7 +55,7 @@ namespace SHADE * none ***************************************************************************/ template - static std::enable_if_t, void> CreateSystem() + static std::enable_if_t, SystemID> CreateSystem() { SystemTypeID typeID = SystemFamily::GetID(); @@ -64,10 +64,12 @@ namespace SHADE while (systemContainer.find(id) != systemContainer.end()) { ++version; - SystemID id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; + id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; } systemContainer.emplace(id, std::make_unique()); + systemContainer[id].get()->systemID = id; + return id; } @@ -103,7 +105,7 @@ namespace SHADE ***************************************************************************/ static void Init() noexcept; - static void RunRoutines(bool editorPause) noexcept; + static void RunRoutines(bool editorPause, double deltaTime) noexcept; template static void RegisterRoutine(SystemVersionID version = 0) noexcept @@ -112,6 +114,7 @@ namespace SHADE if (system == nullptr) return; systemRoutineContainer.emplace_back(std::make_unique()); + systemRoutineContainer.back().get()->system = system; } diff --git a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp index 2c97bfb4..fa34a5cf 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp +++ b/SHADE_Engine/src/ECS_Base/System/SHFixedSystemRoutine.cpp @@ -1,10 +1,11 @@ +#include "SHpch.h" #include "SHFixedSystemRoutine.h" #include "../SHECSMacros.h" namespace SHADE { - void SHFixedSystemRoutine::Execute(double dt) + void SHFixedSystemRoutine::Execute(double dt) noexcept { accumulatedTime += dt; int counter = 0; diff --git a/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h index e294264a..513358ee 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h +++ b/SHADE_Engine/src/ECS_Base/System/SHRoutineStats.h @@ -15,13 +15,14 @@ namespace SHADE double executionTime; int numSteps{1}; - friend std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats); + //friend std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats); }; - std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats) - { - os << stats.name << ": Execution Time: " << stats.executionTime << " Number of steps: " << stats.numSteps << std::endl; - } + //std::ostream& operator<<(std::ostream& os, const SHRoutineStats& stats) + //{ + // os << stats.name << ": Execution Time: " << stats.executionTime << " Number of steps: " << stats.numSteps << std::endl; + // return os; + //} } \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystem.h b/SHADE_Engine/src/ECS_Base/System/SHSystem.h index a2964a91..19b16f72 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystem.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystem.h @@ -10,6 +10,8 @@ #pragma once +#include "../SHECSMacros.h" + namespace SHADE { @@ -17,12 +19,18 @@ namespace SHADE class SHSystem { + private: + SystemID systemID; + protected: /*!************************************************************************* * \brief * Protected default constructor for SHSytem class ***************************************************************************/ SHSystem()= default; + + + public: /*!************************************************************************* @@ -51,37 +59,14 @@ namespace SHADE ***************************************************************************/ virtual void Exit() = 0; + friend class SHSystemManager; + + inline SystemID GetSystemID(void) const noexcept { return systemID; } + inline SystemVersionID GetSystemVersion(void) const noexcept { return static_cast(systemID >> sizeof(SystemVersionID) * CHAR_BIT); } - protected: - }; - - - class SHTestSystem : public SHSystem - { - public: - - SHTestSystem() = default; - ~SHTestSystem() = default; - - int value{5}; - - void Init() - { - - } - - - - void Exit() - { - - } - }; - - } \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp index 83107c60..73157e8b 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemRoutine.cpp @@ -1,5 +1,7 @@ +#include "SHpch.h" #include "SHSystemRoutine.h" + namespace SHADE { diff --git a/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.cpp b/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.cpp new file mode 100644 index 00000000..c9d8477b --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.cpp @@ -0,0 +1,181 @@ +#include "SHpch.h" +#include "SHECSUnitTest.h" +#include "../Managers/SHComponentManager.h" +#include "../Managers/SHEntityManager.h" +#include "../Managers/SHSystemManager.h" +#include "SHTestComponents.h" +#include "SHTestSystems.h" +#include "Tools/SHLogger.h" + + + +namespace SHADE +{ + void SHECSUnitTest::TestAll(void) noexcept + { + TestBasicEntityCreate(); + TestEntityCreateTemplate(); + TestEntityDestroy(); + TestSystemRoutine(); + } + + + void SHECSUnitTest::TestBasicEntityCreate(void) noexcept + { + + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + + SHLOG_INFO("Test for add and remove component") + + EntityID id1 = SHEntityManager::CreateEntity(); + EntityID id2 = SHEntityManager::CreateEntity(); + EntityID id3 = SHEntityManager::CreateEntity(); + + + SHComponentManager::AddComponent(id1); + } + + + void SHECSUnitTest::TestEntityCreateTemplate(void) noexcept + { + std::cout << "\nTest2" << std::endl; + //Test entity Creation. + + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + + + + for (size_t i = 0; i < 10000; ++i) + { + switch (i % 3) + { + case 0: + { + SHEntityManager::CreateEntity(); + }break; + case 1: + { + SHEntityManager::CreateEntity(); + }break; + + case 2: + { + SHEntityManager::CreateEntity(); + }break; + default: + break; + } + + + } + + + auto& denseA = SHComponentManager::GetDense(); + auto& denseB = SHComponentManager::GetDense(); + auto& denseC = SHComponentManager::GetDense(); + + std::cout << "Test Entity Creation" << std::endl; + std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 10000) ? " Success" : " Failure") << std::endl; + std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3334) ? " Success" : " Failure") << std::endl; + std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3333) ? " Success" : " Failure") << std::endl; + std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 10000 ? " Success" : " Failure") << std::endl; + + SHEntityManager::DestroyAllEntity(); + std::cout << std::endl << "Test Destroy All Entity" << std::endl; + std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 0 ? " Success" : " Failure") << std::endl; + + + } + + + void SHECSUnitTest::TestEntityDestroy(void) noexcept + { + std::cout << "\nTest3" << std::endl; + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + + + + for (size_t i = 0; i < 10000; ++i) + { + switch (i % 3) + { + case 0: + { + SHEntityManager::CreateEntity(); + }break; + case 1: + { + SHEntityManager::CreateEntity(); + }break; + + case 2: + { + SHEntityManager::CreateEntity(); + }break; + default: + break; + } + + + } + + SHEntityManager::DestroyEntity(5000); + SHEntityManager::DestroyEntity(5001); + + + + + auto& denseA = SHComponentManager::GetDense(); + auto& denseB = SHComponentManager::GetDense(); + auto& denseC = SHComponentManager::GetDense(); + + + std::cout << "Test Entity Deletion" << std::endl; + std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 9998) ? " Success" : " Failure") << std::endl; + std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3333) ? " Success" : " Failure") << std::endl; + std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3333) ? " Success" : " Failure") << std::endl; + std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 9998 ? " Success" : " Failure") << std::endl; + + + std::cout << std::endl << "Test Entity Recreation" << std::endl; + EntityID id = SHEntityManager::CreateEntity(); + std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 9998) ? " Success" : " Failure") << std::endl; + std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 3333) ? " Success" : " Failure") << std::endl; + std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 3334) ? " Success" : " Failure") << std::endl; + std::cout << "Entity ID: " << id << " EntityIndex: " << EntityHandleGenerator::GetIndex(id) << (EntityHandleGenerator::GetIndex(id) == 5001 ? " Success" : " Failure") << std::endl; + std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 9999 ? " Success" : " Failure") << std::endl; + + + SHEntityManager::DestroyAllEntity(); + std::cout << std::endl << "Check Destroy All Entity" << std::endl; + std::cout << "dense A size: " << denseA.size() << ((denseA.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "dense B size: " << denseB.size() << ((denseB.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "dense C size: " << denseC.size() << ((denseC.size() == 0) ? " Success" : " Failure") << std::endl; + std::cout << "Number of entities: " << SHEntityManager::GetEntityCount() << (SHEntityManager::GetEntityCount() == 0 ? " Success" : " Failure") << std::endl; + + } + + void SHECSUnitTest::TestSystemRoutine(void) noexcept + { + SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); + + + SHSystemManager::RegisterRoutine(1); + + SHSystemManager::RunRoutines(false, 1.0 / 120.0); + + SHSystemManager::Exit(); + } + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.h b/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.h new file mode 100644 index 00000000..938ba865 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/UnitTesting/SHECSUnitTest.h @@ -0,0 +1,22 @@ +#pragma once + +namespace SHADE +{ + class SHECSUnitTest + { + public: + SHECSUnitTest() = delete; + ~SHECSUnitTest() = delete; + + static void TestBasicEntityCreate(void) noexcept; + static void TestEntityCreateTemplate(void) noexcept; + static void TestEntityDestroy(void) noexcept; + static void TestSystemRoutine(void) noexcept; + + static void TestAll(void) noexcept; + + + + + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestComponents.h b/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestComponents.h new file mode 100644 index 00000000..e2e53d6b --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestComponents.h @@ -0,0 +1,30 @@ +#pragma once + +#include "../Components/SHComponent.h" + +namespace SHADE +{ + + class SHComponent_A :public SHComponent + { + public: + int value{}; + + }; + + + class SHComponent_B :public SHComponent + { + public: + float x{}; + float y{}; + float z{}; + }; + + class SHComponent_C :public SHComponent + { + public: + std::string value{}; + }; + +} \ No newline at end of file diff --git a/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestSystems.h b/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestSystems.h new file mode 100644 index 00000000..7231b323 --- /dev/null +++ b/SHADE_Engine/src/ECS_Base/UnitTesting/SHTestSystems.h @@ -0,0 +1,40 @@ +#pragma once + + +#include +#include "../System/SHSystem.h" +#include "../System/SHSystemRoutine.h" + +namespace SHADE +{ + + class SHTestSystem : public SHSystem + { + public: + SHTestSystem() {}; + ~SHTestSystem() {}; + + + + std::string test{ "Test system" }; + + void Init() {}; + void Exit() {}; + + class SHTestRoutine : public SHSystemRoutine + { + public: + SHTestRoutine() + :SHSystemRoutine("Test System Routine", false) {} + + + virtual void Execute(double dt) noexcept + { + + std::cout << GetName() << " System Version: " << GetSystem()->GetSystemVersion() << std::endl; + } + }; + + + }; +} \ No newline at end of file From f0b9f19f4d80c7d9e64e23d69bd2826bdc952f35 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 16 Sep 2022 12:33:42 +0800 Subject: [PATCH 34/36] clear color value fix --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 14 +++++------ .../Graphics/RenderGraph/SHRenderGraph.cpp | 6 +++-- .../Graphics/Renderpass/SHVkRenderpass.cpp | 23 ++++++++++++++----- .../src/Graphics/Renderpass/SHVkRenderpass.h | 5 +++- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 63fd7466..4926ebc0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -98,12 +98,12 @@ namespace SHADE renderGraph.AddResource("Composite", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); renderGraph.AddResource("Downscale", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); renderGraph.AddResource("Present", SH_ATT_DESC_TYPE::COLOR_PRESENT, windowDims.first, windowDims.second); - auto node = renderGraph.AddNode("G-Buffer", { "Position", "Normals", "Composite" }, {}); // no predecessors + auto node = renderGraph.AddNode("G-Buffer", { "Composite", "Position", "Normals", "Present" }, {}); // no predecessors // First subpass to write to G-Buffer auto writeSubpass = node->AddSubpass("G-Buffer Write"); writeSubpass->AddColorOutput("Position"); - writeSubpass->AddColorOutput("Normals"); + writeSubpass->AddColorOutput("Present"); // Second subpass to read from G-Buffer auto compositeSubpass = node->AddSubpass("G-Buffer Composite"); @@ -111,11 +111,11 @@ namespace SHADE compositeSubpass->AddInput("Normals"); compositeSubpass->AddInput("Position"); - //auto compositeNode = renderGraph.AddNode("Bloom", { "Composite", "Downscale", "Present"}, {"G-Buffer"}); - //auto bloomSubpass = compositeNode->AddSubpass("Downsample"); - //bloomSubpass->AddInput("Composite"); - // bloomSubpass->AddColorOutput("Downscale"); - // bloomSubpass->AddColorOutput("Present"); + //auto compositeNode = renderGraph.AddNode("Bloom", { "Composite", "Downscale", "Present" }, { "G-Buffer" }); + //auto bloomSubpass = compositeNode->AddSubpass("Downsample"); + //bloomSubpass->AddInput("Composite"); + //bloomSubpass->AddColorOutput("Downscale"); + //bloomSubpass->AddColorOutput("Present"); renderGraph.Generate(); diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index 76106d67..a2647b74 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -459,10 +459,10 @@ namespace SHADE // We set this to clear first. If later we find out that some predecessor is writing to the same attachment, // we set the pred's storeOp to eStore and "this" loadOp to eLoad. newDesc.loadOp = vk::AttachmentLoadOp::eClear; - newDesc.storeOp = vk::AttachmentStoreOp::eDontCare; + newDesc.storeOp = vk::AttachmentStoreOp::eStore; newDesc.stencilLoadOp = vk::AttachmentLoadOp::eClear; - newDesc.stencilStoreOp = vk::AttachmentStoreOp::eDontCare; + newDesc.stencilStoreOp = vk::AttachmentStoreOp::eStore; newDesc.format = attResources[i]->resourceFormat; @@ -1030,6 +1030,8 @@ namespace SHADE auto& cmdBuffer = commandBuffers[frameIndex]; cmdBuffer->BeginRecording(); + cmdBuffer->SetviewportScissor(1920.0f, 1080.0f, 1920, 1080); + for (auto& node : nodes) { node->Execute(commandBuffers[frameIndex], frameIndex); diff --git a/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.cpp b/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.cpp index 53e3326e..29de5954 100644 --- a/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.cpp +++ b/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.cpp @@ -29,11 +29,17 @@ namespace SHADE /***************************************************************************/ SHVkRenderpass::SHVkRenderpass(Handle const& inLogicalDeviceHdl, std::span const vkDescriptions, std::vector const& subpasses) noexcept : logicalDeviceHdl {inLogicalDeviceHdl} + , numAttDescs {static_cast(vkDescriptions.size())} , clearColors{} { - // TODO: temporary only - clearColors[0].color = { {{0.0f, 0.0f, 0.0f, 1.0f}} }; - clearColors[1].depthStencil = vk::ClearDepthStencilValue(1.0f, 0); + for (uint32_t i = 0; i < vkDescriptions.size(); ++i) + { + if (SHVkUtil::IsDepthStencilAttachment(vkDescriptions[i].format)) + clearColors[i].depthStencil = vk::ClearDepthStencilValue(1.0f, 0); + else + clearColors[i].color = { {{0.0f, 0.0f, 0.0f, 1.0f}} }; + } + vk::RenderPassCreateInfo renderPassCreateInfo{}; std::vector subpassDeps; @@ -164,11 +170,16 @@ namespace SHADE SHVkRenderpass::SHVkRenderpass(Handle const& inLogicalDeviceHdl, std::span const vkDescriptions, std::span const spDescs, std::span const spDeps) noexcept : logicalDeviceHdl{ inLogicalDeviceHdl } + , numAttDescs{ static_cast(vkDescriptions.size()) } , clearColors{} { - // TODO: temporary only - clearColors[0].color = { {{0.0f, 0.0f, 0.0f, 1.0f}} }; - clearColors[1].depthStencil = vk::ClearDepthStencilValue(1.0f, 0); + for (uint32_t i = 0; i < vkDescriptions.size(); ++i) + { + if (SHVkUtil::IsDepthStencilAttachment(vkDescriptions[i].format)) + clearColors[i].depthStencil = vk::ClearDepthStencilValue(1.0f, 0); + else + clearColors[i].color = { {{0.0f, 0.0f, 0.0f, 1.0f}} }; + } subpassDescriptions.resize (spDescs.size()); for (uint32_t i = 0; i < subpassDescriptions.size(); ++i) diff --git a/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.h b/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.h index 6ffd6c38..b0ae7445 100644 --- a/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.h +++ b/SHADE_Engine/src/Graphics/Renderpass/SHVkRenderpass.h @@ -17,7 +17,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* STATIC CONSTEXPR VALUES */ /*-----------------------------------------------------------------------*/ - static constexpr uint32_t NUM_CLEAR_COLORS = 2; + static constexpr uint32_t NUM_CLEAR_COLORS = 10; /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ @@ -34,6 +34,9 @@ namespace SHADE //! Clear colors for the color and depth std::array clearColors; + // number of attachment descriptions + uint32_t numAttDescs; + public: /*-----------------------------------------------------------------------*/ /* CTOR AND DTOR */ From 7315d3038fe32d087a303fdc2ddcbd2d7b07d331 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 16 Sep 2022 14:14:51 +0800 Subject: [PATCH 35/36] Fixed outdated header includes in cxx files Changed references to isActive to function calls --- SHADE_Managed/src/Engine/ECS.cxx | 2 +- SHADE_Managed/src/Engine/ECS.h++ | 4 ++-- SHADE_Managed/src/Engine/ECS.hxx | 2 +- SHADE_Managed/src/Engine/Entity.cxx | 2 +- SHADE_Managed/src/Engine/GameObject.cxx | 6 +++--- SHADE_Managed/src/Scripts/ScriptStore.cxx | 4 ++-- SHADE_Managed/src/Utility/Convert.cxx | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/SHADE_Managed/src/Engine/ECS.cxx b/SHADE_Managed/src/Engine/ECS.cxx index 5aceceee..e4405006 100644 --- a/SHADE_Managed/src/Engine/ECS.cxx +++ b/SHADE_Managed/src/Engine/ECS.cxx @@ -20,7 +20,7 @@ of DigiPen Institute of Technology is prohibited. #include #include // External Dependencies -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" // Project Headers #include "Utility/Convert.hxx" #include "Utility/Debug.hxx" diff --git a/SHADE_Managed/src/Engine/ECS.h++ b/SHADE_Managed/src/Engine/ECS.h++ index e5ede5f2..daaa859f 100644 --- a/SHADE_Managed/src/Engine/ECS.h++ +++ b/SHADE_Managed/src/Engine/ECS.h++ @@ -17,8 +17,8 @@ of DigiPen Institute of Technology is prohibited. // Primary Include #include "ECS.hxx" // External Dependencies -#include "ECS_Base/System/SHComponentManager.h" -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHComponentManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" namespace SHADE { diff --git a/SHADE_Managed/src/Engine/ECS.hxx b/SHADE_Managed/src/Engine/ECS.hxx index 72c88e11..da73f14c 100644 --- a/SHADE_Managed/src/Engine/ECS.hxx +++ b/SHADE_Managed/src/Engine/ECS.hxx @@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited. #pragma once // External Dependencies -#include "ECS_Base/System/SHComponentManager.h" +#include "ECS_Base/Managers/SHComponentManager.h" // Project Includes #include "Components/Component.hxx" diff --git a/SHADE_Managed/src/Engine/Entity.cxx b/SHADE_Managed/src/Engine/Entity.cxx index ba1a31c6..22e8a8c2 100644 --- a/SHADE_Managed/src/Engine/Entity.cxx +++ b/SHADE_Managed/src/Engine/Entity.cxx @@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited. // Primary Header #include "Entity.hxx" // External Dependencies -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" namespace SHADE { diff --git a/SHADE_Managed/src/Engine/GameObject.cxx b/SHADE_Managed/src/Engine/GameObject.cxx index 3896fac5..1fbc3b4a 100644 --- a/SHADE_Managed/src/Engine/GameObject.cxx +++ b/SHADE_Managed/src/Engine/GameObject.cxx @@ -16,7 +16,7 @@ of DigiPen Institute of Technology is prohibited. // Primary Header #include "GameObject.hxx" // External Dependencies -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" // Project Headers #include "ECS.hxx" #include "Scripts/ScriptStore.hxx" @@ -52,7 +52,7 @@ namespace SHADE } bool GameObject::IsActiveSelf::get() { - return GetNativeEntity().isActive; + return GetNativeEntity().GetActive(); } bool GameObject::IsActiveInHierarchy::get() { @@ -68,7 +68,7 @@ namespace SHADE } void GameObject::SetActive(bool active) { - GetNativeEntity().isActive = active; + GetNativeEntity().SetActive(active); } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 9a9eff54..d30cad6b 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited. // Standard Libraries #include // External Dependencies -#include "ECS_Base/System/SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" // Project Headers #include "Utility/Debug.hxx" #include "Utility/Convert.hxx" @@ -668,6 +668,6 @@ namespace SHADE throw gcnew System::InvalidOperationException("Attempted to get native Component to an invalid Entity."); // Check active state - return nativeEntity->isActive; + return nativeEntity->GetActive(); } } diff --git a/SHADE_Managed/src/Utility/Convert.cxx b/SHADE_Managed/src/Utility/Convert.cxx index 8a8aff70..d222fbb3 100644 --- a/SHADE_Managed/src/Utility/Convert.cxx +++ b/SHADE_Managed/src/Utility/Convert.cxx @@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited. // Primary Header #include "Convert.hxx" // External Dependencies -#include "ECS_Base/System//SHEntityManager.h" +#include "ECS_Base/Managers/SHEntityManager.h" #include namespace SHADE From a42a8b0c4626bfbe2e128aa9d17c542c7fd6cfd2 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 16 Sep 2022 14:20:21 +0800 Subject: [PATCH 36/36] SH_API EVERYWHERE --- SHADE_Application/src/Application/SBApplication.cpp | 2 +- SHADE_Engine/src/ECS_Base/System/SHSystemManager.h | 3 ++- .../src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h | 2 +- .../src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h | 3 ++- SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h | 9 +++++---- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 61be1ae0..a378ff36 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -1,6 +1,6 @@ #include "SBpch.h" #include "SBApplication.h" -#include "Engine/ECS_Base/System/SHSystemManager.h" +#include "ECS_Base/System/SHSystemManager.h" #define SHEDITOR #ifdef SHEDITOR diff --git a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h index d5a4866d..d4b960f9 100644 --- a/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h +++ b/SHADE_Engine/src/ECS_Base/System/SHSystemManager.h @@ -20,12 +20,13 @@ #include #include #include "../System/SHSystem.h" +#include "SH_API.h" namespace SHADE { - class SHSystemManager + class SH_API SHSystemManager { //type definition for the container we use to store our system using SystemContainer = std::unordered_map>; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index eeaf6b12..f0a14930 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -63,7 +63,7 @@ namespace SHADE portions of the screen. */ /***********************************************************************************/ - class SHGraphicsSystem : public SHSystem + class SH_API SHGraphicsSystem : public SHSystem { public: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h index 7ed8cb82..04cea4e4 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h @@ -3,6 +3,7 @@ #include #include "SHPerFrameData.h" +#include "SH_API.h" namespace SHADE { @@ -16,7 +17,7 @@ namespace SHADE //! render context in SHADE engine has is that it requires users to call these explicitly in the middle end. While there //! is little reason the flow of the render context should deviate from its intended usage, we want to leave it up to //! users to explicitly call functions from here so we don't risk losing opportunities for different usage. - class SHRenderContext + class SH_API SHRenderContext { private: //! container of frame data. Note that the manager owns the data, but the frame data themselves do not own anything. diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index 91f3f2ca..719c2d46 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -3,6 +3,7 @@ #include "Graphics/Renderpass/SHVkRenderpass.h" #include "Resource/ResourceLibrary.h" +#include "SH_API.h" #include #include @@ -28,7 +29,7 @@ namespace SHADE DEPTH_STENCIL, }; - class SHRenderGraphResource + class SH_API SHRenderGraphResource { private: /*-----------------------------------------------------------------------*/ @@ -74,11 +75,11 @@ namespace SHADE friend class SHRenderGraph; }; - class SHRenderGraphNode : public ISelfHandle + class SH_API SHRenderGraphNode : public ISelfHandle { public: - class SHSubpass + class SH_API SHSubpass { public: /*-----------------------------------------------------------------------*/ @@ -218,7 +219,7 @@ namespace SHADE friend class SHRenderGraph; }; - class SHRenderGraph + class SH_API SHRenderGraph { private: /*-----------------------------------------------------------------------*/