Skip to content

Commit

Permalink
[VK] Move some ImGui stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Apr 14, 2023
1 parent e4c929e commit ad25bde
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
52 changes: 2 additions & 50 deletions src/core/engine/backends/vulkan_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@
namespace gravel {
namespace {

inline constexpr VkDescriptorPoolSize kImGuiDescriptorPoolSizes[] = {
{VK_DESCRIPTOR_TYPE_SAMPLER, 8},
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 32},
{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 32},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 8},
{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 8},
};

[[nodiscard]] auto select_gpu() -> VkPhysicalDevice
{
GRAVEL_ASSERT(vk::get_instance() != VK_NULL_HANDLE);
Expand All @@ -70,11 +56,7 @@ VulkanBackend::VulkanBackend()
mRenderPass {mSwapchain.get_image_format()},
mSampler {vk::create_sampler()},
mPipelineCache {vk::create_pipeline_cache()},
mImGuiPipelineCache {vk::create_pipeline_cache()},
mImGuiDescriptorPool {1'000,
kImGuiDescriptorPoolSizes,
array_length(kImGuiDescriptorPoolSizes),
VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT},
mImGuiData {},
mPipelineBuilder {mPipelineCache.get()}
{
create_shading_pipeline();
Expand Down Expand Up @@ -133,7 +115,7 @@ void VulkanBackend::stop()

void VulkanBackend::on_init(Scene& scene)
{
prepare_imgui_for_vulkan();
vk::init_imgui(mImGuiData, mRenderPass.get(), mSwapchain.get_image_count());

scene.add<vk::ImageCache>();

Expand All @@ -154,36 +136,6 @@ void VulkanBackend::on_init(Scene& scene)
camera_context.active_camera = camera_entity;
}

void VulkanBackend::prepare_imgui_for_vulkan()
{
const auto graphics_queue_family_index =
vk::get_queue_family_indices(vk::get_gpu(), vk::get_surface())
.graphics_family.value();

ImGui_ImplVulkan_InitInfo info {};
info.Instance = mInstance.get();
info.PhysicalDevice = mGPU;
info.Device = mDevice.get();
info.QueueFamily = graphics_queue_family_index;
info.Queue = vk::get_graphics_queue();
info.PipelineCache = mImGuiPipelineCache.get();
info.DescriptorPool = mImGuiDescriptorPool.get();
info.Subpass = 0;
info.MinImageCount = vk::kMaxFramesInFlight;
info.ImageCount = vk::kMaxFramesInFlight;
info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
info.Allocator = nullptr;
info.CheckVkResultFn = [](const VkResult result) {
GRAVEL_VK_CALL(result, "[VK] ImGui Vulkan backend error");
};

if (!ImGui_ImplVulkan_Init(&info, mRenderPass.get())) {
throw Error {"[VK] Could not initialize ImGui Vulkan backend"};
}

DearImGuiVulkan::recreate_font_textures();
}

void VulkanBackend::on_quit()
{
vkDeviceWaitIdle(mDevice.get());
Expand Down
5 changes: 2 additions & 3 deletions src/core/engine/backends/vulkan_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "graphics/vulkan/device.hpp"
#include "graphics/vulkan/frame.hpp"
#include "graphics/vulkan/image/sampler.hpp"
#include "graphics/vulkan/imgui.hpp"
#include "graphics/vulkan/instance.hpp"
#include "graphics/vulkan/model.hpp"
#include "graphics/vulkan/pipeline/descriptor_pool.hpp"
Expand Down Expand Up @@ -78,8 +79,7 @@ class VulkanBackend final : public Backend {
vk::RenderPass mRenderPass;
vk::Sampler mSampler;
vk::PipelineCache mPipelineCache;
vk::PipelineCache mImGuiPipelineCache;
vk::DescriptorPool mImGuiDescriptorPool;
vk::ImGuiData mImGuiData;
vk::CommandPool mCommandPool;
vk::DescriptorSetLayoutBuilder mDescriptorSetLayoutBuilder;
vk::PipelineLayoutBuilder mPipelineLayoutBuilder;
Expand All @@ -96,7 +96,6 @@ class VulkanBackend final : public Backend {

void create_shading_pipeline();
void create_frame_data();
void prepare_imgui_for_vulkan();

void update_static_matrix_buffer(const Camera& camera,
const Transform& camera_transform);
Expand Down
43 changes: 43 additions & 0 deletions src/core/graphics/vulkan/imgui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "imgui.hpp"

#include <imgui_impl_vulkan.h>

#include "common/debug/error.hpp"
#include "graphics/vulkan/context.hpp"
#include "graphics/vulkan/physical_device.hpp"
#include "graphics/vulkan/util/constants.hpp"
#include "graphics/vulkan/util/vk_call.hpp"
#include "init/dear_imgui_vulkan.hpp"

namespace gravel::vk {

void init_imgui(ImGuiData& data, VkRenderPass render_pass, const uint32 swapchain_images)
{
const auto graphics_queue_family_index =
get_queue_family_indices(get_gpu(), get_surface()).graphics_family.value();

ImGui_ImplVulkan_InitInfo info {};
info.Instance = get_instance();
info.PhysicalDevice = get_gpu();
info.Device = get_device();
info.QueueFamily = graphics_queue_family_index;
info.Queue = get_graphics_queue();
info.PipelineCache = data.pipeline_cache.get();
info.DescriptorPool = data.descriptor_pool.get();
info.Subpass = 0;
info.MinImageCount = kMaxFramesInFlight;
info.ImageCount = swapchain_images;
info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
info.Allocator = nullptr;
info.CheckVkResultFn = [](const VkResult result) {
GRAVEL_VK_CALL(result, "[VK] ImGui Vulkan backend error");
};

if (!ImGui_ImplVulkan_Init(&info, render_pass)) {
throw Error {"[VK] Could not initialize ImGui Vulkan backend"};
}

DearImGuiVulkan::recreate_font_textures();
}

} // namespace gravel::vk
38 changes: 38 additions & 0 deletions src/core/graphics/vulkan/imgui.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <array> // size

#include <vulkan/vulkan.h>

#include "common/primitives.hpp"
#include "graphics/vulkan/pipeline/descriptor_pool.hpp"
#include "graphics/vulkan/pipeline/pipeline_cache.hpp"

namespace gravel::vk {

inline constexpr VkDescriptorPoolSize kImGuiDescriptorPoolSizes[] = {
{VK_DESCRIPTOR_TYPE_SAMPLER, 8},
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 32},
{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 32},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 8},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 8},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 8},
{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 8},
};

struct ImGuiData final {
PipelineCache pipeline_cache {create_pipeline_cache()};
DescriptorPool descriptor_pool {1'000,
kImGuiDescriptorPoolSizes,
std::size(kImGuiDescriptorPoolSizes),
VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT};
};

/// Initializes the ImGui Vulkan backend.
void init_imgui(ImGuiData& data, VkRenderPass render_pass, uint32 swapchain_images);

} // namespace gravel::vk

0 comments on commit ad25bde

Please sign in to comment.