Skip to content

Commit 54afe28

Browse files
committed
ImGui setup
1 parent eb10003 commit 54afe28

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "libs/glTF-Sample-Models"]
1414
path = libs/glTF-Sample-Models
1515
url = https://github.com/KhronosGroup/glTF-Sample-Models.git
16+
[submodule "libs/imgui"]
17+
path = libs/imgui
18+
url = https://github.com/ocornut/imgui.git

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 11)
88
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
99

1010
set(SOURCE_FILES src/main.cpp src/simple_logger.cpp src/model.cpp src/vulkan_base/vulkan_device.cpp src/vulkan_base/vulkan_swapchain.cpp src/vulkan_base/vulkan_renderpass.cpp src/vulkan_base/vulkan_pipeline.cpp src/vulkan_base/vulkan_utils.cpp)
11+
set(IMGUI_FILES libs/imgui/imgui.cpp libs/imgui/imgui_demo.cpp libs/imgui/imgui_draw.cpp libs/imgui/imgui_tables.cpp libs/imgui/imgui_widgets.cpp libs/imgui/backends/imgui_impl_sdl.cpp libs/imgui/backends/imgui_impl_vulkan.cpp)
1112

1213
# Find SDL2
1314
add_subdirectory(libs/SDL)
@@ -30,10 +31,11 @@ add_custom_target(build_shaders ALL
3031
endif(WIN32)
3132

3233
# vulkan_tutorial executable
33-
add_executable(vulkan_tutorial ${SOURCE_FILES})
34+
add_executable(vulkan_tutorial ${SOURCE_FILES} ${IMGUI_FILES})
3435
add_dependencies(vulkan_tutorial build_shaders)
3536
target_include_directories(vulkan_tutorial PUBLIC libs/SDL/include)
3637
target_include_directories(vulkan_tutorial PUBLIC libs)
38+
target_include_directories(vulkan_tutorial PUBLIC libs/imgui)
3739
target_link_libraries(vulkan_tutorial PUBLIC SDL2-static)
3840
target_include_directories(vulkan_tutorial PUBLIC ${Vulkan_INCLUDE_DIRS})
3941
target_link_libraries(vulkan_tutorial PUBLIC ${Vulkan_LIBRARIES})

libs/imgui

Submodule imgui added at 91b356c

src/main.cpp

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "vulkan_base/vulkan_base.h"
1414
#include "model.h"
1515

16+
#include <imgui.h>
17+
#include <backends/imgui_impl_sdl.h>
18+
#include <backends/imgui_impl_vulkan.h>
19+
1620
#define FRAMES_IN_FLIGHT 2
1721

1822
VulkanContext* context;
@@ -46,6 +50,8 @@ VulkanBuffer modelUniformBuffers[FRAMES_IN_FLIGHT];
4650

4751
VkQueryPool timestampQueryPools[FRAMES_IN_FLIGHT];
4852

53+
VkDescriptorPool imguiDescriptorPool;
54+
4955
struct Camera {
5056
glm::vec3 cameraPosition;
5157
glm::vec3 cameraDirection;
@@ -58,17 +64,21 @@ struct Camera {
5864
} camera;
5965

6066
bool handleMessage() {
67+
ImGuiIO& io = ImGui::GetIO();
68+
6169
SDL_Event event;
6270
while (SDL_PollEvent(&event)) {
71+
ImGui_ImplSDL2_ProcessEvent(&event);
72+
6373
switch (event.type) {
6474
case SDL_QUIT:
6575
return false;
6676
case SDL_KEYDOWN:
67-
if(event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
77+
if(event.key.keysym.scancode == SDL_SCANCODE_ESCAPE && !io.WantCaptureKeyboard) {
6878
SDL_SetRelativeMouseMode(SDL_FALSE);
6979
}
7080
case SDL_MOUSEBUTTONDOWN:
71-
if(event.button.button == SDL_BUTTON_LEFT) {
81+
if(event.button.button == SDL_BUTTON_LEFT && !io.WantCaptureMouse) {
7282
SDL_SetRelativeMouseMode(SDL_TRUE);
7383
}
7484
break;
@@ -362,6 +372,69 @@ void initApplication(SDL_Window* window) {
362372
camera.yaw = 0.0f;
363373
camera.pitch = 0.0f;
364374
}
375+
376+
// Init ImGui
377+
{
378+
VkDescriptorPoolSize poolSizes[] = {
379+
{ VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
380+
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
381+
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
382+
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
383+
{ VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
384+
{ VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
385+
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
386+
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
387+
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
388+
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
389+
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
390+
};
391+
VkDescriptorPoolCreateInfo poolInfo = {};
392+
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
393+
poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
394+
poolInfo.maxSets = 1000 * ARRAY_COUNT(poolSizes);
395+
poolInfo.poolSizeCount = (uint32_t)ARRAY_COUNT(poolSizes);
396+
poolInfo.pPoolSizes = poolSizes;
397+
VKA(vkCreateDescriptorPool(context->device, &poolInfo, 0, &imguiDescriptorPool));
398+
}
399+
ImGui::CreateContext();
400+
ImGui::StyleColorsDark();
401+
ImGui_ImplSDL2_InitForVulkan(window);
402+
ImGui_ImplVulkan_InitInfo initInfo = {};
403+
initInfo.Instance = context->instance;
404+
initInfo.PhysicalDevice = context->physicalDevice;
405+
initInfo.Device = context->device;
406+
initInfo.QueueFamily = context->graphicsQueue.familyIndex;
407+
initInfo.Queue = context->graphicsQueue.queue;
408+
initInfo.DescriptorPool = imguiDescriptorPool;
409+
initInfo.MinImageCount = 2;
410+
initInfo.ImageCount = swapchain.images.size();
411+
initInfo.MSAASamples = VK_SAMPLE_COUNT_4_BIT;
412+
ImGui_ImplVulkan_Init(&initInfo, renderPass);
413+
// Upload Fonts
414+
{
415+
// Use any command queue
416+
VkCommandPool commandPool = commandPools[0];
417+
VkCommandBuffer commandBuffer = commandBuffers[0];
418+
419+
VKA(vkResetCommandPool(context->device, commandPool, 0));
420+
VkCommandBufferBeginInfo begin_info = {};
421+
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
422+
begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
423+
VKA(vkBeginCommandBuffer(commandBuffer, &begin_info));
424+
425+
ImGui_ImplVulkan_CreateFontsTexture(commandBuffer);
426+
427+
VKA(vkEndCommandBuffer(commandBuffer));
428+
429+
VkSubmitInfo submitInfo = {};
430+
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
431+
submitInfo.commandBufferCount = 1;
432+
submitInfo.pCommandBuffers = &commandBuffer;
433+
VKA(vkQueueSubmit(context->graphicsQueue.queue, 1, &submitInfo, VK_NULL_HANDLE));
434+
435+
VKA(vkDeviceWaitIdle(context->device));
436+
ImGui_ImplVulkan_DestroyFontUploadObjects();
437+
}
365438
}
366439

367440
void recreateSwapchain() {
@@ -484,6 +557,10 @@ void renderApplication() {
484557
vkCmdDrawIndexed(commandBuffer, model.numIndices, 1, 0, 0, 0);
485558
#endif
486559

560+
ImGui::Render();
561+
ImDrawData* drawData = ImGui::GetDrawData();
562+
ImGui_ImplVulkan_RenderDrawData(drawData, commandBuffer);
563+
487564
vkCmdEndRenderPass(commandBuffer);
488565

489566
VK(vkCmdWriteTimestamp(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, timestampQueryPools[frameIndex], 1));
@@ -522,6 +599,12 @@ void renderApplication() {
522599
void shutdownApplication() {
523600
VKA(vkDeviceWaitIdle(context->device));
524601

602+
// Destroy ImGui
603+
ImGui_ImplVulkan_Shutdown();
604+
VK(vkDestroyDescriptorPool(context->device, imguiDescriptorPool, 0));
605+
ImGui_ImplSDL2_Shutdown();
606+
ImGui::DestroyContext();
607+
525608
VK(vkDestroyDescriptorPool(context->device, modelDescriptorPool, 0));
526609
VK(vkDestroyDescriptorSetLayout(context->device, modelDescriptorSetLayout, 0));
527610
destroyModel(context, &model);
@@ -572,6 +655,10 @@ void shutdownApplication() {
572655
}
573656

574657
void updateApplication(float delta) {
658+
ImGui_ImplVulkan_NewFrame();
659+
ImGui_ImplSDL2_NewFrame();
660+
ImGui::NewFrame();
661+
575662
const uint8_t* keys = SDL_GetKeyboardState(0);
576663
int mouseX, mouseY;
577664
uint32_t mouseButtons = SDL_GetRelativeMouseState(&mouseX, &mouseY);
@@ -613,6 +700,11 @@ void updateApplication(float delta) {
613700
camera.proj = getProjectionInverseZ(glm::radians(45.0f), swapchain.width, swapchain.height, 0.01f);
614701
camera.view = glm::lookAtLH(camera.cameraPosition, camera.cameraPosition + camera.cameraDirection, camera.up);
615702
camera.viewProj = camera.proj * camera.view;
703+
704+
static bool showDemoWindow = true;
705+
if(showDemoWindow) {
706+
ImGui::ShowDemoWindow(&showDemoWindow);
707+
}
616708
}
617709

618710
int main() {

0 commit comments

Comments
 (0)