Repository navigation
Expand file tree
/
Copy pathimgui_layer.cpp
More file actions
82 lines (70 loc) · 3.01 KB
/
Copy pathimgui_layer.cpp
File metadata and controls
82 lines (70 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "imgui_layer.h"
#include "renderer.h"
#include "renderer_unsafe.h"
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_vulkan.h>
#include <cstdlib>
#include <iostream>
namespace {
void checkVk(VkResult result) {
if (result == VK_SUCCESS) return;
std::cerr << "ImGui Vulkan error: " << result << '\n';
if (result < 0) std::abort();
}
// Runs inside NRE's render pass on the window, after the scene.
void drawImGui(VkCommandBuffer cmd) {
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd);
}
}
namespace ImGuiLayer {
void init() {
using namespace NRE;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// No ImGuiConfigFlags_NavEnableKeyboard: with it, a focused ImGui window claims the keyboard
// (WantCaptureKeyboard), so the camera would ignore Tab/WASD after any click on the UI.
ImGui::StyleColorsDark();
// Chains to the GLFW callbacks NRE already installed, so NRE's input keeps working.
ImGui_ImplGlfw_InitForVulkan(Unsafe::getWindow(), true);
// NRE renders with dynamic rendering: one color attachment (swapchain format) and a depth attachment.
static VkFormat colorFormat = Unsafe::getSwapchainFormat();
ImGui_ImplVulkan_InitInfo info{};
info.ApiVersion = VK_API_VERSION_1_3;
info.Instance = Unsafe::getInstance();
info.PhysicalDevice = Unsafe::getPhysicalDevice();
info.Device = Unsafe::getDevice();
info.QueueFamily = Unsafe::getGraphicsQueueFamily();
info.Queue = Unsafe::getGraphicsQueue();
// ImGui's own font atlas plus room for textures added with ImGui_ImplVulkan_AddTexture (e.g. NRE's render target).
info.DescriptorPoolSize = IMGUI_IMPL_VULKAN_MINIMUM_SAMPLED_IMAGE_POOL_SIZE + 16;
info.ImageCount = Unsafe::getSwapchainImageCount();
info.MinImageCount = info.ImageCount < 2 ? 2 : info.ImageCount;
info.UseDynamicRendering = true;
info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
auto& rendering = info.PipelineInfoMain.PipelineRenderingCreateInfo;
rendering.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR;
rendering.colorAttachmentCount = 1;
rendering.pColorAttachmentFormats = &colorFormat;
rendering.depthAttachmentFormat = Unsafe::getDepthFormat();
info.CheckVkResultFn = checkVk;
ImGui_ImplVulkan_Init(&info);
(void)Unsafe::registerFrameCallback(drawImGui);
}
void shutdown() {
(void)NRE::Unsafe::registerFrameCallback(nullptr);
vkDeviceWaitIdle(NRE::Unsafe::getDevice());
ImGui_ImplVulkan_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void beginFrame() {
ImGui_ImplVulkan_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void endFrame() {
// Built before renderFrame(): NRE records it through drawImGui during the frame.
ImGui::Render();
}
}