From a8d00a29ec2c20298b4be831ee0c75b18bffc0b1 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Fri, 9 Sep 2022 16:04:49 +0800 Subject: [PATCH] [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 +}