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
1822VulkanContext* context;
@@ -46,6 +50,8 @@ VulkanBuffer modelUniformBuffers[FRAMES_IN_FLIGHT];
4650
4751VkQueryPool timestampQueryPools[FRAMES_IN_FLIGHT];
4852
53+ VkDescriptorPool imguiDescriptorPool;
54+
4955struct Camera {
5056 glm::vec3 cameraPosition;
5157 glm::vec3 cameraDirection;
@@ -58,17 +64,21 @@ struct Camera {
5864} camera;
5965
6066bool 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
367440void 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() {
522599void 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
574657void 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
618710int main () {
0 commit comments