Changeset 1cb64e6 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Feb 14, 2021, 4:04:56 AM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
ea2b4dc
Parents:
737c26a
Message:

In VulkanGame, recreate the command buffers every frame

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r737c26a r1cb64e6  
    5656   // Also, decide whether to use this-> for all instance variables, or only when necessary
    5757
    58    this->debugMessenger = VK_NULL_HANDLE;
    59 
    60    this->gui = nullptr;
    61    this->window = nullptr;
    62 
    63    this->swapChainPresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
    64    this->swapChainMinImageCount = 0;
    65 
    66    this->currentFrame = 0;
     58   debugMessenger = VK_NULL_HANDLE;
     59
     60   gui = nullptr;
     61   window = nullptr;
     62
     63   swapChainPresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
     64   swapChainMinImageCount = 0;
     65
     66   currentFrame = 0;
    6767   shouldRecreateSwapChain = false;
    6868
    69    this->object_VP_mats = {};
    70    this->ship_VP_mats = {};
    71    this->asteroid_VP_mats = {};
    72    this->laser_VP_mats = {};
    73    this->explosion_UBO = {};
     69   object_VP_mats = {};
     70   ship_VP_mats = {};
     71   asteroid_VP_mats = {};
     72   laser_VP_mats = {};
     73   explosion_UBO = {};
    7474}
    7575
     
    108108   currentScreen->init();
    109109
     110   // TODO: Maybe just set shouldRecreateSwapChain to true instead. Check this render loop logic
     111   // to make sure there'd be no issues
    110112   recreateSwapChain();
    111113}
     
    218220   createImageViews();
    219221   createRenderPass();
     222
    220223   createResourceCommandPool();
    221224   createCommandPools();
     
    11411144
    11421145void VulkanGame::cleanup() {
    1143    if (vkDeviceWaitIdle(device) != VK_SUCCESS) {
    1144       throw runtime_error("failed to wait for device!");
    1145    }
     1146   VKUTIL_CHECK_RESULT(vkDeviceWaitIdle(device), "failed to wait for device!");
    11461147
    11471148   ImGui_ImplVulkan_Shutdown();
     
    12811282   cerr << "validation layer: " << pCallbackData->pMessage << endl;
    12821283
     1284   // TODO: Figure out what the return value means and if it should always be VK_FALSE
    12831285   return VK_FALSE;
    12841286}
     
    16831685      }
    16841686   }
    1685 
    1686    for (size_t i = 0; i < commandBuffers.size(); i++) {
    1687       VkCommandBufferBeginInfo beginInfo = {};
    1688       beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    1689       beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
    1690       beginInfo.pInheritanceInfo = nullptr;
    1691 
    1692       if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
    1693          throw runtime_error("failed to begin recording command buffer!");
    1694       }
    1695 
    1696       VkRenderPassBeginInfo renderPassInfo = {};
    1697       renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
    1698       renderPassInfo.renderPass = renderPass;
    1699       renderPassInfo.framebuffer = swapChainFramebuffers[i];
    1700       renderPassInfo.renderArea.offset = { 0, 0 };
    1701       renderPassInfo.renderArea.extent = swapChainExtent;
    1702 
    1703       array<VkClearValue, 2> clearValues = {};
    1704       clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
    1705       clearValues[1].depthStencil = { 1.0f, 0 };
    1706 
    1707       renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
    1708       renderPassInfo.pClearValues = clearValues.data();
    1709 
    1710       vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    1711 
    1712       currentScreen->createRenderCommands(commandBuffers[i], i);
    1713 
    1714       /**********************************************************/
    1715 
    1716       ImGui_ImplVulkan_NewFrame();
    1717       ImGui_ImplSDL2_NewFrame(this->window);
    1718       ImGui::NewFrame();
    1719 
    1720       {
    1721          ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
    1722          ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
    1723          ImGui::Begin("WndMenubar", NULL,
    1724             ImGuiWindowFlags_NoTitleBar |
    1725             ImGuiWindowFlags_NoResize |
    1726             ImGuiWindowFlags_NoMove);
    1727          ImGui::InvisibleButton("", ImVec2(155, 18));
    1728          ImGui::SameLine();
    1729          if (ImGui::Button("Main Menu")) {
    1730             cout << "Clicked on the main button" << endl;
    1731             //events.push(Event::GO_TO_MAIN_MENU);
    1732          }
    1733          ImGui::End();
    1734       }
    1735 
    1736       ImGui::Render();
    1737       ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), commandBuffers[i]);
    1738 
    1739       /**********************************************************/
    1740 
    1741       vkCmdEndRenderPass(commandBuffers[i]);
    1742 
    1743       if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
    1744          throw runtime_error("failed to record command buffer!");
    1745       }
    1746    }
    17471687}
    17481688
     
    17661706
    17671707   updateScene();
     1708
     1709   VKUTIL_CHECK_RESULT(vkResetCommandPool(device, commandPools[imageIndex], 0),
     1710      "failed to reset command pool!");
     1711
     1712   VkCommandBufferBeginInfo beginInfo = {};
     1713   beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
     1714   beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
     1715   beginInfo.pInheritanceInfo = nullptr;
     1716
     1717   if (vkBeginCommandBuffer(commandBuffers[imageIndex], &beginInfo) != VK_SUCCESS) {
     1718      throw runtime_error("failed to begin recording command buffer!");
     1719   }
     1720
     1721   VkRenderPassBeginInfo renderPassInfo = {};
     1722   renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
     1723   renderPassInfo.renderPass = renderPass;
     1724   renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex];
     1725   renderPassInfo.renderArea.offset = { 0, 0 };
     1726   renderPassInfo.renderArea.extent = swapChainExtent;
     1727
     1728   array<VkClearValue, 2> clearValues = {};
     1729   clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
     1730   clearValues[1].depthStencil = { 1.0f, 0 };
     1731
     1732   renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
     1733   renderPassInfo.pClearValues = clearValues.data();
     1734
     1735   vkCmdBeginRenderPass(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
     1736
     1737   currentScreen->createRenderCommands(commandBuffers[imageIndex], imageIndex);
     1738
     1739   /**********************************************************/
     1740
     1741   ImGui_ImplVulkan_NewFrame();
     1742   ImGui_ImplSDL2_NewFrame(this->window);
     1743   ImGui::NewFrame();
     1744
     1745   {
     1746      ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
     1747      ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
     1748      ImGui::Begin("WndMenubar", NULL,
     1749         ImGuiWindowFlags_NoTitleBar |
     1750         ImGuiWindowFlags_NoResize |
     1751         ImGuiWindowFlags_NoMove);
     1752      ImGui::InvisibleButton("", ImVec2(155, 18));
     1753      ImGui::SameLine();
     1754      if (ImGui::Button("Main Menu")) {
     1755         cout << "Clicked on the main button" << endl;
     1756         //events.push(Event::GO_TO_MAIN_MENU);
     1757      }
     1758      ImGui::End();
     1759   }
     1760
     1761   ImGui::Render();
     1762   ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), commandBuffers[imageIndex]);
     1763
     1764   /**********************************************************/
     1765
     1766   vkCmdEndRenderPass(commandBuffers[imageIndex]);
     1767
     1768   if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS) {
     1769      throw runtime_error("failed to record command buffer!");
     1770   }
    17681771
    17691772   VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] };
     
    17811784   submitInfo.pSignalSemaphores = signalSemaphores;
    17821785
    1783    if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) {
    1784       throw runtime_error("failed to submit draw command buffer!");
    1785    }
     1786   VKUTIL_CHECK_RESULT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]),
     1787      "failed to submit draw command buffer!");
    17861788}
    17871789
     
    18001802   VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);
    18011803
    1802    if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || shouldRecreateSwapChain) {
    1803       shouldRecreateSwapChain = false;
    1804       recreateSwapChain();
    1805    } else if (result != VK_SUCCESS) {
    1806       throw runtime_error("failed to present swap chain image!");
     1804   if (result == VK_SUBOPTIMAL_KHR) {
     1805      shouldRecreateSwapChain = true;
     1806   } else if (result == VK_ERROR_OUT_OF_DATE_KHR) {
     1807      shouldRecreateSwapChain = true;
     1808      return;
     1809   } else {
     1810      VKUTIL_CHECK_RESULT(result, "failed to present swap chain image!");
    18071811   }
    18081812
     
    21252129}
    21262130
    2127 // TODO: Fix the crash that happens when alt-tabbing
    21282131void VulkanGame::recreateSwapChain() {
    2129    cout << "Recreating swap chain" << endl;
    2130    gui->refreshWindowSize();
    2131 
    2132    while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
    2133       (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
    2134       SDL_WaitEvent(nullptr);
    2135       gui->refreshWindowSize();
    2136    }
    2137 
    21382132   if (vkDeviceWaitIdle(device) != VK_SUCCESS) {
    21392133      throw runtime_error("failed to wait for device!");
Note: See TracChangeset for help on using the changeset viewer.