Changeset e469aed in opengl-game


Ignore:
Timestamp:
Apr 7, 2021, 1:38:54 AM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
5081b9a
Parents:
cefdf23
Message:

Modify SDLGame to use createImageResources, initImGuiOverlay, and cleanupImGuiOverlay

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.cpp

    rcefdf23 re469aed  
    6060                     , shouldRecreateSwapChain(false)
    6161                     , frameCount(0)
    62                      , currentFrame()
     62                     , currentFrame(0)
    6363                     , imageIndex(0)
    6464                     , fpsStartTime(0.0f)
     
    6666                     , done(false)
    6767                     , currentRenderScreenFn(nullptr)
    68                      , descriptorPool(VK_NULL_HANDLE)
     68                     , imguiDescriptorPool(VK_NULL_HANDLE)
    6969                     , gui(nullptr)
    7070                     , window(nullptr)
     
    7777
    7878void VulkanGame::run(int width, int height, unsigned char guiFlags) {
    79    // TODO: Maybe call the init code in the constructor instead of in run()
    80    // Research this
    8179   cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
    8280
    8381   cout << "Vulkan Game" << endl;
    8482
    85    // TODO: Move IMGUI initialization in here
    8683   if (initUI(width, height, guiFlags) == RTWO_ERROR) {
    8784      return;
     
    9087   initVulkan();
    9188
    92    // Create Descriptor Pool
    93    {
    94       VkDescriptorPoolSize pool_sizes[] =
    95       {
    96           { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
    97           { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
    98           { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
    99           { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
    100           { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
    101           { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
    102           { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
    103           { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
    104           { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
    105           { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
    106           { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
    107       };
    108       VkDescriptorPoolCreateInfo pool_info = {};
    109       pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
    110       pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
    111       pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes);
    112       pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes);
    113       pool_info.pPoolSizes = pool_sizes;
    114 
    115       VKUTIL_CHECK_RESULT(vkCreateDescriptorPool(device, &pool_info, nullptr, &descriptorPool),
    116          "failed to create descriptor pool");
    117    }
    118 
    119    // TODO: Do this in one place and save it instead of redoing it every time I need a queue family index
    120    QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface);
    121 
    122    // Setup Dear ImGui context
    123    IMGUI_CHECKVERSION();
    124    ImGui::CreateContext();
     89   initImGuiOverlay();
     90
     91   currentRenderScreenFn = &VulkanGame::renderMainScreen;
     92
    12593   ImGuiIO& io = ImGui::GetIO();
    126    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    127    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
    128 
    129    // Setup Dear ImGui style
    130    ImGui::StyleColorsDark();
    131    //ImGui::StyleColorsClassic();
    132 
    133    // Setup Platform/Renderer bindings
    134    ImGui_ImplSDL2_InitForVulkan(window);
    135    ImGui_ImplVulkan_InitInfo init_info = {};
    136    init_info.Instance = instance;
    137    init_info.PhysicalDevice = physicalDevice;
    138    init_info.Device = device;
    139    init_info.QueueFamily = indices.graphicsFamily.value();
    140    init_info.Queue = graphicsQueue;
    141    init_info.DescriptorPool = descriptorPool;
    142    init_info.Allocator = nullptr;
    143    init_info.MinImageCount = swapChainMinImageCount;
    144    init_info.ImageCount = swapChainImageCount;
    145    init_info.CheckVkResultFn = check_imgui_vk_result;
    146    ImGui_ImplVulkan_Init(&init_info, renderPass);
    147 
    148    // Load Fonts
    149    // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
    150    // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
    151    // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
    152    // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
    153    // - Read 'docs/FONTS.md' for more instructions and details.
    154    // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
    155    //io.Fonts->AddFontDefault();
    156    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
    157    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
    158    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
    159    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
    160    //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
    161    //assert(font != NULL);
    162 
    163    // Upload Fonts
    164    {
    165       VkCommandBuffer commandBuffer = VulkanUtils::beginSingleTimeCommands(device, resourceCommandPool);
    166 
    167       ImGui_ImplVulkan_CreateFontsTexture(commandBuffer);
    168 
    169       VulkanUtils::endSingleTimeCommands(device, resourceCommandPool, commandBuffer, graphicsQueue);
    170 
    171       ImGui_ImplVulkan_DestroyFontUploadObjects();
    172    }
    173 
    174    currentRenderScreenFn = &VulkanGame::renderMainScreen;
    17594
    17695   initGuiValueLists(valueLists);
     
    221140      cout << "UI library could not be initialized!" << endl;
    222141      cout << gui->getError() << endl;
     142      // TODO: Rename RTWO_ERROR to something else
    223143      return RTWO_ERROR;
    224144   }
     
    253173   createSwapChain();
    254174   createImageViews();
     175
     176   createResourceCommandPool();
     177   createImageResources();
     178
    255179   createRenderPass();
    256 
    257    createResourceCommandPool();
    258180   createCommandPools();
    259 
    260    VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,
    261       depthImage, graphicsQueue);
    262 
    263181   createFramebuffers();
    264 
    265    // TODO: Initialize pipelines here
    266 
    267182   createCommandBuffers();
    268 
    269183   createSyncObjects();
    270184}
     
    354268      }
    355269
    356       // Start the Dear ImGui frame
     270      // TODO: Move this into a renderImGuiOverlay() function
    357271      ImGui_ImplVulkan_NewFrame();
    358272      ImGui_ImplSDL2_NewFrame(window);
     
    378292   VKUTIL_CHECK_RESULT(vkDeviceWaitIdle(device), "failed to wait for device!");
    379293
    380    ImGui_ImplVulkan_Shutdown();
    381    ImGui_ImplSDL2_Shutdown();
    382    ImGui::DestroyContext();
     294   cleanupImGuiOverlay();
    383295
    384296   cleanupSwapChain();
    385 
    386    // this would actually be destroyed in the pipeline class
    387    vkDestroyDescriptorPool(device, descriptorPool, nullptr);
    388297
    389298   vkDestroyCommandPool(device, resourceCommandPool, nullptr);
     
    702611         VK_IMAGE_ASPECT_COLOR_BIT);
    703612   }
     613}
     614
     615void VulkanGame::createResourceCommandPool() {
     616   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface);
     617
     618   VkCommandPoolCreateInfo poolInfo = {};
     619   poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
     620   poolInfo.queueFamilyIndex = indices.graphicsFamily.value();
     621   poolInfo.flags = 0;
     622
     623   if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) {
     624      throw runtime_error("failed to create resource command pool!");
     625   }
     626}
     627
     628void VulkanGame::createImageResources() {
     629   VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,
     630      depthImage, graphicsQueue);
     631}
     632
     633VkFormat VulkanGame::findDepthFormat() {
     634   return VulkanUtils::findSupportedFormat(
     635      physicalDevice,
     636      { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
     637      VK_IMAGE_TILING_OPTIMAL,
     638      VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
     639   );
    704640}
    705641
     
    766702}
    767703
    768 VkFormat VulkanGame::findDepthFormat() {
    769    return VulkanUtils::findSupportedFormat(
    770       physicalDevice,
    771       { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
    772       VK_IMAGE_TILING_OPTIMAL,
    773       VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
    774    );
    775 }
    776 
    777 void VulkanGame::createResourceCommandPool() {
    778    QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface);
    779 
    780    VkCommandPoolCreateInfo poolInfo = {};
    781    poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    782    poolInfo.queueFamilyIndex = indices.graphicsFamily.value();
    783    poolInfo.flags = 0;
    784 
    785    if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) {
    786       throw runtime_error("failed to create resource command pool!");
    787    }
    788 }
    789 
    790704void VulkanGame::createCommandPools() {
    791705   commandPools.resize(swapChainImageCount);
     
    859773
    860774   for (size_t i = 0; i < swapChainImageCount; i++) {
    861       if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]) != VK_SUCCESS) {
    862          throw runtime_error("failed to create image acquired sempahore for a frame!");
    863       }
    864 
    865       if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]) != VK_SUCCESS) {
    866          throw runtime_error("failed to create render complete sempahore for a frame!");
    867       }
    868 
    869       if (vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
    870          throw runtime_error("failed to create fence for a frame!");
    871       }
    872    }
     775      VKUTIL_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]),
     776         "failed to create image acquired sempahore for a frame!");
     777
     778      VKUTIL_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]),
     779         "failed to create render complete sempahore for a frame!");
     780
     781      VKUTIL_CHECK_RESULT(vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]),
     782         "failed to create fence for a frame!");
     783   }
     784}
     785
     786void VulkanGame::initImGuiOverlay() {
     787   vector<VkDescriptorPoolSize> pool_sizes {
     788       { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
     789       { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
     790       { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
     791       { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
     792       { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
     793       { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
     794       { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
     795       { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
     796       { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
     797       { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
     798       { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
     799   };
     800
     801   VkDescriptorPoolCreateInfo pool_info = {};
     802   pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
     803   pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
     804   pool_info.maxSets = 1000 * pool_sizes.size();
     805   pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size());
     806   pool_info.pPoolSizes = pool_sizes.data();
     807
     808   VKUTIL_CHECK_RESULT(vkCreateDescriptorPool(device, &pool_info, nullptr, &imguiDescriptorPool),
     809      "failed to create IMGUI descriptor pool!");
     810
     811   // TODO: Do this in one place and save it instead of redoing it every time I need a queue family index
     812   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface);
     813
     814   // Setup Dear ImGui context
     815   IMGUI_CHECKVERSION();
     816   ImGui::CreateContext();
     817   ImGuiIO& io = ImGui::GetIO();
     818   //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
     819   //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
     820
     821   // Setup Dear ImGui style
     822   ImGui::StyleColorsDark();
     823   //ImGui::StyleColorsClassic();
     824
     825   // Setup Platform/Renderer bindings
     826   ImGui_ImplSDL2_InitForVulkan(window);
     827   ImGui_ImplVulkan_InitInfo init_info = {};
     828   init_info.Instance = instance;
     829   init_info.PhysicalDevice = physicalDevice;
     830   init_info.Device = device;
     831   init_info.QueueFamily = indices.graphicsFamily.value();
     832   init_info.Queue = graphicsQueue;
     833   init_info.DescriptorPool = imguiDescriptorPool;
     834   init_info.Allocator = nullptr;
     835   init_info.MinImageCount = swapChainMinImageCount;
     836   init_info.ImageCount = swapChainImageCount;
     837   init_info.CheckVkResultFn = check_imgui_vk_result;
     838   ImGui_ImplVulkan_Init(&init_info, renderPass);
     839
     840   // Load Fonts
     841   // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
     842   // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
     843   // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
     844   // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
     845   // - Read 'docs/FONTS.md' for more instructions and details.
     846   // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
     847   //io.Fonts->AddFontDefault();
     848   //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
     849   //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
     850   //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
     851   //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
     852   //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
     853   //assert(font != NULL);
     854
     855   // Upload Fonts
     856
     857   VkCommandBuffer commandBuffer = VulkanUtils::beginSingleTimeCommands(device, resourceCommandPool);
     858
     859   ImGui_ImplVulkan_CreateFontsTexture(commandBuffer);
     860
     861   VulkanUtils::endSingleTimeCommands(device, resourceCommandPool, commandBuffer, graphicsQueue);
     862
     863   ImGui_ImplVulkan_DestroyFontUploadObjects();
     864}
     865
     866void VulkanGame::cleanupImGuiOverlay() {
     867   ImGui_ImplVulkan_Shutdown();
     868   ImGui_ImplSDL2_Shutdown();
     869   ImGui::DestroyContext();
     870
     871   vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr);
    873872}
    874873
     
    898897   VkCommandBufferBeginInfo beginInfo = {};
    899898   beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    900    beginInfo.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
     899   beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
    901900
    902901   VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers[imageIndex], &beginInfo),
     
    907906   renderPassInfo.renderPass = renderPass;
    908907   renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex];
     908   renderPassInfo.renderArea.offset = { 0, 0 };
    909909   renderPassInfo.renderArea.extent = swapChainExtent;
    910910
    911911   array<VkClearValue, 2> clearValues = {};
    912    clearValues[0].color = { { 0.45f, 0.55f, 0.60f, 1.00f } };
     912   clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
    913913   clearValues[1].depthStencil = { 1.0f, 0 };
    914914
     
    978978   createSwapChain();
    979979   createImageViews();
    980    createRenderPass();
    981 
    982    createCommandPools();
    983980
    984981   // The depth buffer does need to be recreated with the swap chain since its dimensions depend on the window size
     
    987984      depthImage, graphicsQueue);
    988985
     986   createRenderPass();
     987   createCommandPools();
    989988   createFramebuffers();
     989   createCommandBuffers();
     990   createSyncObjects();
    990991
    991992   // TODO: Update pipelines here
    992 
    993    createCommandBuffers();
    994 
    995    createSyncObjects();
    996993
    997994   imageIndex = 0;
  • sdl-game.hpp

    rcefdf23 re469aed  
    107107      bool shouldRecreateSwapChain;
    108108
     109      // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
     110      VkDescriptorPool imguiDescriptorPool;
     111
    109112      /*** High-level vars ***/
    110113
     
    118121
    119122      // TODO: Make a separate singleton Timer class
    120       // It could also deal with the steady_clock vs high_resolution_clock issue
    121123      time_point<steady_clock> startTime;
    122124      float fpsStartTime, curTime;
     
    142144      void createSwapChain();
    143145      void createImageViews();
     146      void createResourceCommandPool();
     147      void createImageResources();
     148      VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
    144149      void createRenderPass();
    145       VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
    146       void createResourceCommandPool();
    147150      void createCommandPools();
    148151      void createFramebuffers();
    149152      void createCommandBuffers();
    150153      void createSyncObjects();
     154
     155      void initImGuiOverlay();
     156      void cleanupImGuiOverlay();
    151157
    152158      void renderFrame(ImDrawData* draw_data);
     
    167173      void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
    168174      void quitGame();
    169 
    170       // Pipeline variables. Hopefully, I can eventually use the GraphicsPipeline_Vulkan class for the imgui pipeline
    171       VkDescriptorPool descriptorPool;
    172175};
    173176
Note: See TracChangeset for help on using the changeset viewer.