From f9fb74ba6acb46fe687ba9235b8c6ddf8816d504 Mon Sep 17 00:00:00 2001 From: Thraix Date: Thu, 13 Jul 2023 21:17:42 +0200 Subject: [PATCH] Add Image rendering support for ImGui - Fix alpha channel bug being set to 0 --- CopiumEngine/res/shaders/passthrough.frag | 2 +- CopiumEngine/res/shaders/shader.frag | 2 +- CopiumEngine/src/copium/core/Application.cpp | 8 +- CopiumEngine/src/copium/core/Application.h | 1 + .../src/copium/core/ImGuiInstance.cpp | 94 +++++++++++++------ CopiumEngine/src/copium/core/ImGuiInstance.h | 8 ++ CopiumEngine/src/copium/pipeline/Pipeline.cpp | 2 +- 7 files changed, 83 insertions(+), 34 deletions(-) diff --git a/CopiumEngine/res/shaders/passthrough.frag b/CopiumEngine/res/shaders/passthrough.frag index 48c1f4f..f09ade6 100644 --- a/CopiumEngine/res/shaders/passthrough.frag +++ b/CopiumEngine/res/shaders/passthrough.frag @@ -8,5 +8,5 @@ layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(texture(texSampler, inTexCoord).rgb, 1.0); + outColor = texture(texSampler, inTexCoord); } \ No newline at end of file diff --git a/CopiumEngine/res/shaders/shader.frag b/CopiumEngine/res/shaders/shader.frag index 0a7d88b..31bf261 100644 --- a/CopiumEngine/res/shaders/shader.frag +++ b/CopiumEngine/res/shaders/shader.frag @@ -12,5 +12,5 @@ layout(location = 0) out vec4 outColor; void main() { float scale = 0.45 + max(dot(vec3(0, 1, 0), normalize(inLightPos - inPosition)), 0.0); - outColor = vec4(inColor, 1.0) * texture(texSampler, inTexCoord) * scale; + outColor = vec4(inColor * texture(texSampler, inTexCoord).rgb * scale, 1.0); } diff --git a/CopiumEngine/src/copium/core/Application.cpp b/CopiumEngine/src/copium/core/Application.cpp index f3119d7..672080e 100644 --- a/CopiumEngine/src/copium/core/Application.cpp +++ b/CopiumEngine/src/copium/core/Application.cpp @@ -93,6 +93,7 @@ namespace Copium const WindowResizeEvent& windowResizeEvent = static_cast(event); AssetManager::GetAsset(framebuffer).Resize(windowResizeEvent.GetWidth(), windowResizeEvent.GetHeight()); descriptorSetPassthrough->SetSampler(AssetManager::GetAsset(framebuffer).GetColorAttachment(), 0); + descriptorSetImGui->SetSampler(AssetManager::GetAsset(framebuffer).GetColorAttachment(), 0); return EventResult::Continue; } @@ -146,6 +147,9 @@ namespace Copium descriptorSetPassthrough = AssetManager::GetAsset(graphicsPipelinePassthrough).CreateDescriptorSet(*descriptorPool, 0); descriptorSetPassthrough->SetSampler(AssetManager::GetAsset(framebuffer).GetColorAttachment(), 0); + + descriptorSetImGui = Vulkan::GetImGuiInstance().CreateDescriptorSet(); + descriptorSetImGui->SetSampler(AssetManager::GetAsset(framebuffer).GetColorAttachment(), 0); } void Application::InitializeGraphicsPipeline() @@ -168,8 +172,10 @@ namespace Copium void Application::RecordCommandBuffer() { Vulkan::GetImGuiInstance().Begin(); - ImGui::ShowDemoWindow(); commandBuffer->Begin(); + ImGui::Begin("Viewport"); + ImGui::Image(*descriptorSetImGui, ImVec2{480, 270}, ImVec2{0, 1}, ImVec2{1, 0}); + ImGui::End(); Framebuffer& fb = AssetManager::GetAsset(framebuffer); Pipeline& pl = AssetManager::GetAsset(graphicsPipeline); diff --git a/CopiumEngine/src/copium/core/Application.h b/CopiumEngine/src/copium/core/Application.h index 5f7fb62..c96ee92 100644 --- a/CopiumEngine/src/copium/core/Application.h +++ b/CopiumEngine/src/copium/core/Application.h @@ -25,6 +25,7 @@ namespace Copium std::unique_ptr descriptorPool; std::unique_ptr descriptorSet; std::unique_ptr descriptorSetPassthrough; + std::unique_ptr descriptorSetImGui; std::unique_ptr scene; std::unique_ptr mesh; std::unique_ptr meshPassthrough; diff --git a/CopiumEngine/src/copium/core/ImGuiInstance.cpp b/CopiumEngine/src/copium/core/ImGuiInstance.cpp index d303092..c795213 100644 --- a/CopiumEngine/src/copium/core/ImGuiInstance.cpp +++ b/CopiumEngine/src/copium/core/ImGuiInstance.cpp @@ -12,40 +12,13 @@ namespace Copium ImGuiInstance::ImGuiInstance() : descriptorPool{std::make_unique()} { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - - ImGui_ImplGlfw_InitForVulkan(Vulkan::GetWindow().GetWindow(), true); - ImGui_ImplVulkan_InitInfo init_info = {}; - init_info.Instance = Vulkan::GetInstance(); - init_info.PhysicalDevice = Vulkan::GetDevice().GetPhysicalDevice(); - init_info.Device = Vulkan::GetDevice(); - init_info.QueueFamily = Vulkan::GetDevice().GetGraphicsQueueFamily(); - init_info.Queue = Vulkan::GetDevice().GetGraphicsQueue(); - init_info.PipelineCache = VK_NULL_HANDLE; - init_info.DescriptorPool = *descriptorPool.get(); - init_info.Subpass = 0; - init_info.MinImageCount = SwapChain::MAX_FRAMES_IN_FLIGHT; - init_info.ImageCount = Vulkan::GetSwapChain().GetImageCount(); - init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT; - init_info.Allocator = nullptr; - init_info.CheckVkResultFn = CheckVkResult; - ImGui_ImplVulkan_Init(&init_info, Vulkan::GetSwapChain().GetRenderPass()); - { - CommandBufferScoped commandBuffer; - ImGui_ImplVulkan_CreateFontsTexture(commandBuffer); - } - ImGui_ImplVulkan_DestroyFontUploadObjects(); + InitializeImGui(); + InitializeDescriptorSetLayout(); } ImGuiInstance::~ImGuiInstance() { + vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr); ImGui_ImplVulkan_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); @@ -70,6 +43,67 @@ namespace Copium ImGui_ImplVulkan_RenderDrawData(draw_data, commandBuffer); } + std::unique_ptr ImGuiInstance::CreateDescriptorSet() + { + return std::make_unique(*descriptorPool, descriptorSetLayout, shaderBindings); + } + + void ImGuiInstance::InitializeImGui() + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + ImGui::StyleColorsDark(); + + ImGui_ImplGlfw_InitForVulkan(Vulkan::GetWindow().GetWindow(), true); + ImGui_ImplVulkan_InitInfo initInfo = {}; + initInfo.Instance = Vulkan::GetInstance(); + initInfo.PhysicalDevice = Vulkan::GetDevice().GetPhysicalDevice(); + initInfo.Device = Vulkan::GetDevice(); + initInfo.QueueFamily = Vulkan::GetDevice().GetGraphicsQueueFamily(); + initInfo.Queue = Vulkan::GetDevice().GetGraphicsQueue(); + initInfo.PipelineCache = VK_NULL_HANDLE; + initInfo.DescriptorPool = *descriptorPool.get(); + initInfo.Subpass = 0; + initInfo.MinImageCount = SwapChain::MAX_FRAMES_IN_FLIGHT; + initInfo.ImageCount = Vulkan::GetSwapChain().GetImageCount(); + initInfo.MSAASamples = VK_SAMPLE_COUNT_1_BIT; + initInfo.Allocator = nullptr; + initInfo.CheckVkResultFn = CheckVkResult; + ImGui_ImplVulkan_Init(&initInfo, Vulkan::GetSwapChain().GetRenderPass()); + { + CommandBufferScoped commandBuffer; + ImGui_ImplVulkan_CreateFontsTexture(commandBuffer); + } + ImGui_ImplVulkan_DestroyFontUploadObjects(); + } + + void ImGuiInstance::InitializeDescriptorSetLayout() + { + std::vector layoutBindings{1}; + layoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + layoutBindings[0].descriptorCount = 1; + layoutBindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; + VkDescriptorSetLayoutCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + createInfo.bindingCount = 1; + createInfo.pBindings = layoutBindings.data(); + CP_VK_ASSERT(vkCreateDescriptorSetLayout(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorSetLayout), "Failed to create ImGui DescriptorSetLayout"); + + ShaderBinding binding; + binding.name = "texture"; + binding.set = 0; + binding.binding = 0; + binding.arraySize = 1; + binding.bindingType = BindingType::Sampler2D; + binding.shaderType = ShaderType::Fragment; + + shaderBindings = {binding}; + } + void ImGuiInstance::CheckVkResult(VkResult err) { CP_VK_ASSERT(err, "Failed to initialize ImGui"); diff --git a/CopiumEngine/src/copium/core/ImGuiInstance.h b/CopiumEngine/src/copium/core/ImGuiInstance.h index 4cd6d4b..0cb6a5c 100644 --- a/CopiumEngine/src/copium/core/ImGuiInstance.h +++ b/CopiumEngine/src/copium/core/ImGuiInstance.h @@ -4,6 +4,7 @@ #include "copium/buffer/CommandBuffer.h" #include "copium/pipeline/DescriptorPool.h" +#include "copium/pipeline/DescriptorSet.h" namespace Copium { @@ -13,6 +14,8 @@ namespace Copium private: std::unique_ptr descriptorPool; + VkDescriptorSetLayout descriptorSetLayout; + std::set shaderBindings; public: ImGuiInstance(); @@ -22,7 +25,12 @@ namespace Copium void End(); void Render(CommandBuffer& commandBuffer); + std::unique_ptr CreateDescriptorSet(); + private: + void InitializeImGui(); + void InitializeDescriptorSetLayout(); + static void CheckVkResult(VkResult err); }; } diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index 40c1e97..c64e08f 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -216,7 +216,7 @@ namespace Copium colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; - colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE; colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; VkPipelineColorBlendStateCreateInfo colorBlendCreateInfo{};