Changeset 3b7d497 in opengl-game


Ignore:
Timestamp:
Jan 2, 2021, 4:19:31 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
6493e43
Parents:
c324d6a
Message:

Start implementing an ImGUI ui on top of SDL and Vulkan using some example code

Files:
7 added
4 edited

Legend:

Unmodified
Added
Removed
  • VulkanGame.vcxproj

    rc324d6a r3b7d497  
    148148    <ClCompile Include="gui\screen.cpp" />
    149149    <ClCompile Include="gui\ui-element.cpp" />
     150    <ClCompile Include="IMGUI\imgui.cpp" />
     151    <ClCompile Include="IMGUI\imgui_demo.cpp" />
     152    <ClCompile Include="IMGUI\imgui_draw.cpp" />
     153    <ClCompile Include="IMGUI\imgui_impl_sdl.cpp" />
     154    <ClCompile Include="IMGUI\imgui_impl_vulkan.cpp" />
     155    <ClCompile Include="IMGUI\imgui_widgets.cpp" />
    150156    <ClCompile Include="logger.cpp" />
    151157    <ClCompile Include="main-vulkan.cpp" />
     158    <ClCompile Include="sdl-game.cpp" />
    152159    <ClCompile Include="StackWalker.cpp" />
    153160    <ClCompile Include="utils.cpp" />
     
    170177    <ClInclude Include="gui\screen.hpp" />
    171178    <ClInclude Include="gui\ui-element.hpp" />
     179    <ClInclude Include="IMGUI\imconfig.h" />
     180    <ClInclude Include="IMGUI\imgui.h" />
     181    <ClInclude Include="IMGUI\imgui_impl_sdl.h" />
     182    <ClInclude Include="IMGUI\imgui_impl_vulkan.h" />
     183    <ClInclude Include="IMGUI\imgui_internal.h" />
     184    <ClInclude Include="IMGUI\imstb_rectpack.h" />
     185    <ClInclude Include="IMGUI\imstb_textedit.h" />
     186    <ClInclude Include="IMGUI\imstb_truetype.h" />
    172187    <ClInclude Include="logger.hpp" />
     188    <ClInclude Include="sdl-game.hpp" />
    173189    <ClInclude Include="StackWalker.h" />
    174190    <ClInclude Include="utils.hpp" />
  • main-vulkan.cpp

    rc324d6a r3b7d497  
    44#include "crash-logger.hpp"
    55
    6 #include "vulkan-game.hpp"
     6//#include "vulkan-game.hpp"
     7#include "sdl-game.hpp"
    78
    89using namespace std;
     
    2526
    2627   try {
    27       game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
     28      //game.run(800, 600, 0);
     29      //game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
     30      game.run(1280, 720, 0);
    2831   } catch (const exception& e) {
    2932      cerr << e.what() << endl;
  • vulkan-game.cpp

    rc324d6a r3b7d497  
    11#include "vulkan-game.hpp"
     2
     3#include "IMGUI/imgui_impl_sdl.h"
     4#include "IMGUI/imgui_impl_vulkan.h"
    25
    36#include <array>
     
    2528 * - IMGUI creates one command pool per framebuffer
    2629 */
     30
     31/* NOTES WHEN ADDING IMGUI
     32 *
     33 * Possibly cleanup the imgui pipeline in cleanupSwapchain or call some imgui function that does this for me
     34 * call ImGui_ImplVulkan_RenderDrawData, without passing in a pipeline, to do the rendering
     35 */
     36
     37// Put in here to use for IMGUI, but I might use something similar in other places as well
     38static void check_vk_result(VkResult result) {
     39   if (result == VK_SUCCESS)
     40      return;
     41   fprintf(stderr, "[vulkan] Error: VkResult = %d\n", result);
     42   if (result < 0)
     43      abort();
     44}
    2745
    2846VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
     
    189207   createImageResources();
    190208   createFramebuffers();
     209
     210   // TODO: I think I can start setting up IMGUI here
     211   // ImGui_ImplVulkan_Init will create the Vulkan pipeline for ImGui for me
     212   // imgui_impl_vulkan keeps track of the imgui pipeline internally
     213   // TODO: Check how the example recreates the pipeline and what code I need
     214   // to copy over to do that
     215
     216   createImguiDescriptorPool();
     217
     218   IMGUI_CHECKVERSION();
     219   ImGui::CreateContext();
     220   ImGuiIO& io = ImGui::GetIO(); (void)io;
     221   //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
     222   //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
     223
     224   // Setup Dear ImGui style
     225   ImGui::StyleColorsDark();
     226   //ImGui::StyleColorsClassic();
     227
     228   // TODO: Make call this once and save the results since it's also called when creating the logical device
     229   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
     230
     231   ImGui_ImplSDL2_InitForVulkan(window);
     232   ImGui_ImplVulkan_InitInfo init_info = {};
     233   init_info.Instance = this->instance;
     234   init_info.PhysicalDevice = this->physicalDevice;
     235   init_info.Device = this->device;
     236   init_info.QueueFamily = indices.graphicsFamily.value();
     237   init_info.Queue = this->graphicsQueue;
     238   init_info.PipelineCache = VK_NULL_HANDLE;
     239   init_info.DescriptorPool = this->imguiDescriptorPool; // TODO: Create a descriptor pool for IMGUI
     240   init_info.Allocator = nullptr;
     241   init_info.MinImageCount = this->swapChainImageCount;
     242   init_info.ImageCount = this->swapChainImages.size();
     243   init_info.CheckVkResultFn = check_vk_result;
     244   ImGui_ImplVulkan_Init(&init_info, this->renderPass);
     245
     246   cout << "Got here" << endl;
     247
     248   // TODO: I think I have code in VkUtil for creating VkImages, which uses command buffers
     249   // Maybe check how that code works
     250
     251   // Upload Fonts
     252   {
     253      VkResult err;
     254
     255      // Use any command queue
     256      VkCommandPool command_pool = this->commandPool; // TODO: No need to create a separate variable. Just use this->commandPool directly
     257      VkCommandBuffer command_buffer;
     258
     259      // Create the command buffer to load
     260      VkCommandBufferAllocateInfo info = {};
     261      info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
     262      info.commandPool = command_pool;
     263      info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
     264      info.commandBufferCount = 1;
     265      err = vkAllocateCommandBuffers(this->device, &info, &command_buffer);
     266      check_vk_result(err);
     267
     268      //err = vkResetCommandPool(this->device, command_pool, 0); // Probably not really needed here since the command pool is never used before this
     269      //check_vk_result(err);
     270      VkCommandBufferBeginInfo begin_info = {};
     271      begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
     272      begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
     273      err = vkBeginCommandBuffer(command_buffer, &begin_info);
     274      check_vk_result(err);
     275
     276      ImGui_ImplVulkan_CreateFontsTexture(command_buffer);
     277
     278      VkSubmitInfo end_info = {};
     279      end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
     280      end_info.commandBufferCount = 1;
     281      end_info.pCommandBuffers = &command_buffer;
     282      err = vkEndCommandBuffer(command_buffer);
     283      check_vk_result(err);
     284      err = vkQueueSubmit(this->graphicsQueue, 1, &end_info, VK_NULL_HANDLE);
     285      check_vk_result(err);
     286
     287      err = vkDeviceWaitIdle(this->device);
     288      check_vk_result(err);
     289      ImGui_ImplVulkan_DestroyFontUploadObjects();
     290
     291      // This should make the command pool reusable for later
     292      err = vkResetCommandPool(this->device, command_pool, 0);
     293      check_vk_result(err);
     294   }
     295
     296   cout << "And now here" << endl;
    191297
    192298   initMatrices();
     
    584690
    585691   createSyncObjects();
     692
     693   cout << "Finished init function" << endl;
    586694}
    587695
     
    10861194
    10871195void VulkanGame::cleanup() {
     1196   ImGui_ImplVulkan_Shutdown();
     1197   ImGui_ImplSDL2_Shutdown();
     1198   ImGui::DestroyContext();
     1199
     1200   destroyImguiDescriptorPool();
     1201
    10881202   cleanupSwapChain();
    10891203
     
    15921706      currentScreen->createRenderCommands(commandBuffers[i], i);
    15931707
     1708      /**********************************************************/
     1709
     1710      ImGui_ImplVulkan_NewFrame();
     1711      ImGui_ImplSDL2_NewFrame(this->window);
     1712      ImGui::NewFrame();
     1713
     1714      {
     1715         ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
     1716         ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
     1717         ImGui::Begin("WndMenubar", NULL,
     1718            ImGuiWindowFlags_NoTitleBar |
     1719            ImGuiWindowFlags_NoResize |
     1720            ImGuiWindowFlags_NoMove);
     1721         ImGui::InvisibleButton("", ImVec2(155, 18));
     1722         ImGui::SameLine();
     1723         if (ImGui::Button("Main Menu")) {
     1724            cout << "Clicked on the main button" << endl;
     1725            //events.push(Event::GO_TO_MAIN_MENU);
     1726         }
     1727         ImGui::End();
     1728      }
     1729
     1730      ImGui::Render();
     1731      ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), commandBuffers[i]);
     1732
     1733      /**********************************************************/
     1734
    15941735      vkCmdEndRenderPass(commandBuffers[i]);
    15951736
     
    15981739      }
    15991740   }
     1741}
     1742
     1743void VulkanGame::createImguiDescriptorPool() {
     1744   vector<VkDescriptorPoolSize> pool_sizes{
     1745       { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
     1746       { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
     1747       { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
     1748       { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
     1749       { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
     1750       { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
     1751       { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
     1752       { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
     1753       { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
     1754       { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
     1755       { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
     1756   };
     1757
     1758   VkDescriptorPoolCreateInfo pool_info = {};
     1759   pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
     1760   pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
     1761   pool_info.maxSets = 1000 * pool_sizes.size();
     1762   pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size());
     1763   pool_info.pPoolSizes = pool_sizes.data();
     1764   if (vkCreateDescriptorPool(device, &pool_info, nullptr, &imguiDescriptorPool) != VK_SUCCESS) {
     1765      throw runtime_error("failed to create IMGUI descriptor pool!");
     1766   }
     1767}
     1768
     1769void VulkanGame::destroyImguiDescriptorPool() {
     1770   vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr);
    16001771}
    16011772
  • vulkan-game.hpp

    rc324d6a r3b7d497  
    289289
    290290      bool framebufferResized;
     291
     292      VkDescriptorPool imguiDescriptorPool;
    291293
    292294      VkSampler textureSampler;
     
    402404      void createSyncObjects();
    403405
     406      void createImguiDescriptorPool();
     407      void destroyImguiDescriptorPool();
     408
    404409      // TODO: Since addObject() returns a reference to the new object now,
    405410      // stop using objects.back() to access the object that was just created
Note: See TracChangeset for help on using the changeset viewer.