Changeset 9c0a614 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Feb 14, 2021, 2:15:18 AM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
4e2c709
Parents:
9067efc
git-author:
Dmitry Portnoy <dportnoy@…> (02/14/21 02:06:31)
git-committer:
Dmitry Portnoy <dportnoy@…> (02/14/21 02:15:18)
Message:

Switch to using one command pool per swap chain image in VulkanGame

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r9067efc r9c0a614  
    219219   createRenderPass();
    220220   createResourceCommandPool();
    221    createCommandPool();
     221   createCommandPools();
    222222
    223223   createImageResources();
     
    12291229
    12301230   vkDestroyCommandPool(device, resourceCommandPool, nullptr);
    1231    vkDestroyCommandPool(device, commandPool, nullptr);
    12321231
    12331232   vkDestroyDevice(device, nullptr);
     
    16321631}
    16331632
    1634 void VulkanGame::createCommandPool() {
    1635    QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    1636 
    1637    VkCommandPoolCreateInfo poolInfo = {};
    1638    poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    1639    poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
    1640    poolInfo.flags = 0;
    1641 
    1642    if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
    1643       throw runtime_error("failed to create graphics command pool!");
     1633void VulkanGame::createCommandPools() {
     1634   commandPools.resize(swapChainImageCount);
     1635
     1636   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
     1637
     1638   for (size_t i = 0; i < swapChainImageCount; i++) {
     1639      VkCommandPoolCreateInfo poolInfo = {};
     1640      poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
     1641      poolInfo.queueFamilyIndex = indices.graphicsFamily.value();
     1642      poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
     1643
     1644      VKUTIL_CHECK_RESULT(vkCreateCommandPool(device, &poolInfo, nullptr, &commandPools[i]),
     1645         "failed to create graphics command pool!");
    16441646   }
    16451647}
     
    17311733   commandBuffers.resize(swapChainImageCount);
    17321734
    1733    VkCommandBufferAllocateInfo allocInfo = {};
    1734    allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    1735    allocInfo.commandPool = commandPool;
    1736    allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    1737    allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
    1738 
    1739    if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
    1740       throw runtime_error("failed to allocate command buffers!");
     1735   for (size_t i = 0; i < swapChainImageCount; i++) {
     1736      VkCommandBufferAllocateInfo allocInfo = {};
     1737      allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
     1738      allocInfo.commandPool = commandPools[i];
     1739      allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
     1740      allocInfo.commandBufferCount = 1;
     1741
     1742      if (vkAllocateCommandBuffers(device, &allocInfo, &commandBuffers[i]) != VK_SUCCESS) {
     1743         throw runtime_error("failed to allocate command buffer!");
     1744      }
    17411745   }
    17421746
     
    21402144   createRenderPass();
    21412145
     2146   createCommandPools();
     2147
     2148   // The depth buffer does need to be recreated with the swap chain since its dimensions depend on the window size
     2149   // and resizing the window is a common reason to recreate the swapchain
    21422150   VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,
    21432151      depthImage, graphicsQueue);
     
    22052213   }
    22062214
    2207    vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     2215   for (uint32_t i = 0; i < swapChainImageCount; i++) {
     2216      vkFreeCommandBuffers(device, commandPools[i], 1, &commandBuffers[i]);
     2217      vkDestroyCommandPool(device, commandPools[i], nullptr);
     2218   }
    22082219
    22092220   overlayPipeline.cleanup();
Note: See TracChangeset for help on using the changeset viewer.