diff --git a/.clangd b/.clangd index 4cc5202..9c2b0e0 100644 --- a/.clangd +++ b/.clangd @@ -6,6 +6,7 @@ CompileFlags: - -DINITIALIZERS_IMPL - -DPIPELINES_IMPL - -DDESCRIPTORS_IMPL + - -DCALLBACK_IMPL - -DTIMER_IMPL - -DLOGGER_IMPL - -DSERVER_INIT_IMPL diff --git a/src/Engine/engine/engine.h b/src/Engine/engine/engine.h index 05f3774..56b8b2b 100644 --- a/src/Engine/engine/engine.h +++ b/src/Engine/engine/engine.h @@ -109,10 +109,15 @@ class Engine { uint32_t width, uint32_t height ); + void buildImgui(); void destroySwapchain(); void drawBackground( VkCommandBuffer cmd ); + void drawImgui( + VkCommandBuffer cmd, + VkImageView targetImageView + ); }; @@ -325,6 +330,19 @@ void Engine::draw() { _swapchainExtent ); + // Set the swapchain image layout to Attachment Optimal so ImGui can write on top of it + vkutil::transitionImage( + cmd, + _swapchainImages[swapchainImageIndex], + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL + ); + + drawImgui( + cmd, + _swapchainImageViews[swapchainImageIndex] + ); + // Uses the present layout to display into the screen vkutil::transitionImage( cmd, @@ -384,6 +402,7 @@ void Engine::run() { continue; } + buildImgui(); draw(); } #ifdef DEBUG @@ -890,7 +909,7 @@ void Engine::initImgui() { .PipelineRenderingCreateInfo { .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR, .colorAttachmentCount = 1, - .pColorAttachmentFormats = &_swapchainImageFormat + .pColorAttachmentFormats = &_drawImage.imageFormat } }, .UseDynamicRendering = true @@ -955,6 +974,19 @@ void Engine::createSwapchain( #endif } +void Engine::buildImgui() { + // New frames + ImGui_ImplVulkan_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + // Main ImGui things + ImGui::ShowDemoWindow(); + + // Finish everything + ImGui::Render(); +} + void Engine::destroySwapchain() { #ifdef DEBUG fmt::println("[Engine:Cleanup] Destroying the swapchain..."); @@ -1011,6 +1043,38 @@ void Engine::drawBackground( } +void Engine::drawImgui( + VkCommandBuffer cmd, + VkImageView targetImageView +) { + VkRenderingAttachmentInfo colorAttachment { + vkinit::attachmentInfo( + targetImageView, + nullptr, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL + ) + }; + VkRenderingInfo renderInfo { + vkinit::renderingInfo( + _swapchainExtent, + &colorAttachment, + nullptr + ) + }; + + vkCmdBeginRendering( + cmd, + &renderInfo + ); + + ImGui_ImplVulkan_RenderDrawData( + ImGui::GetDrawData(), + cmd + ); + + vkCmdEndRendering(cmd); +} + void Engine::immediateSubmit( std::function< void(VkCommandBuffer cmd) @@ -1080,5 +1144,7 @@ void Engine::immediateSubmit( } + + #endif #endif diff --git a/src/Engine/engine/initializers.h b/src/Engine/engine/initializers.h index 517ae62..ed807e6 100644 --- a/src/Engine/engine/initializers.h +++ b/src/Engine/engine/initializers.h @@ -2,6 +2,7 @@ #define INITIALIZERS_H // Normal header #include "Common/Types.h" +#include namespace vkinit { VkCommandPoolCreateInfo commandPoolCreateInfo( @@ -57,6 +58,17 @@ namespace vkinit { VkImageAspectFlags aspectFlags ); + VkRenderingAttachmentInfo attachmentInfo( + VkImageView view, + VkClearValue* clear, + VkImageLayout layout /*= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL*/ + ); + + VkRenderingInfo renderingInfo( + VkExtent2D renderExtent, + VkRenderingAttachmentInfo* colorAttachment, + VkRenderingAttachmentInfo* depthAttachment + ); } @@ -242,6 +254,52 @@ namespace vkinit { return info; } + + VkRenderingAttachmentInfo attachmentInfo( + VkImageView view, + VkClearValue* clear, + VkImageLayout layout /*= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL*/ + ) { + VkRenderingAttachmentInfo colorAttachment { + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .pNext = nullptr, + .imageView = view, + .imageLayout = layout, + .loadOp = clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE + }; + + if (clear) { + colorAttachment.clearValue = *clear; + } + + return colorAttachment; + } + + VkRenderingInfo renderingInfo( + VkExtent2D renderExtent, + VkRenderingAttachmentInfo* colorAttachment, + VkRenderingAttachmentInfo* depthAttachment + ) { + VkRenderingInfo renderInfo { + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, + .pNext = nullptr, + .renderArea = VkRect2D { + VkOffset2D { + 0, + 0 + }, renderExtent + }, + .layerCount = 1, + .colorAttachmentCount = 1, + .pColorAttachments = colorAttachment, + .pDepthAttachment = depthAttachment, + .pStencilAttachment = nullptr + }; + return renderInfo; + } + + } #endif #endif