Changeset 3f32dfd in opengl-game


Ignore:
Timestamp:
Jan 24, 2021, 5:56:07 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
ce9dc9f
Parents:
7f60b28
git-author:
Dmitry Portnoy <dportnoy@…> (01/24/21 17:47:00)
git-committer:
Dmitry Portnoy <dportnoy@…> (01/24/21 17:56:07)
Message:

In VulkanGame, use the rewritten functions for querting swap chain capabilities, and use a separate command pool for setting up image resources

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r7f60b28 r3f32dfd  
    1818
    1919using namespace std;
     20
     21// TODO: Update all instances of the "... != VK_SUCCESS" check to something similar to
     22// the agp error checking function, which prints an appropriate error message based on the error code
    2023
    2124// TODO: Update all occurances of instance variables to use this-> (Actually, not sure if I really want to do this)
     
    4447}
    4548
    46 VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
     49VulkanGame::VulkanGame() {
     50   // TODO: Double-check whether initialization should happen in the header, where the variables are declared, or here
     51   // Also, decide whether to use this-> for all instance variables, or only when necessary
     52
     53   this->debugMessenger = VK_NULL_HANDLE;
     54
    4755   this->gui = nullptr;
    4856   this->window = nullptr;
    4957
    50    this->debugMessenger = VK_NULL_HANDLE;
     58   this->swapChainPresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
     59   this->swapChainMinImageCount = 0;
    5160
    5261   this->currentFrame = 0;
     
    200209   pickPhysicalDevice(deviceExtensions);
    201210   createLogicalDevice(validationLayers, deviceExtensions);
     211   chooseSwapChainProperties();
    202212   createSwapChain();
    203213   createImageViews();
    204214   createRenderPass();
     215   createResourceCommandPool();
    205216   createCommandPool();
    206217
     
    226237   //ImGui::StyleColorsClassic();
    227238
    228    // TODO: Make call this once and save the results since it's also called when creating the logical device
     239   // TODO: Maybe call this once and save the results since it's also called when creating the logical device
    229240   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    230241
     
    234245   init_info.PhysicalDevice = this->physicalDevice;
    235246   init_info.Device = this->device;
    236    init_info.PipelineCache = VK_NULL_HANDLE;
     247   init_info.QueueFamily = indices.graphicsFamily.value();
     248   init_info.Queue = graphicsQueue;
    237249   init_info.DescriptorPool = this->imguiDescriptorPool; // TODO: Create a descriptor pool for IMGUI
    238250   init_info.Allocator = nullptr;
    239    init_info.MinImageCount = this->swapChainImageCount;
    240    init_info.ImageCount = this->swapChainImages.size();
     251   init_info.MinImageCount = this->swapChainMinImageCount;
     252   init_info.ImageCount = this->swapChainImageCount;
    241253   init_info.CheckVkResultFn = check_vk_result;
    242254   ImGui_ImplVulkan_Init(&init_info, this->renderPass);
     
    251263      VkResult err;
    252264
    253       // Use any command queue
    254       VkCommandPool command_pool = this->commandPool; // TODO: No need to create a separate variable. Just use this->commandPool directly
    255265      VkCommandBuffer command_buffer;
    256266
     
    258268      VkCommandBufferAllocateInfo info = {};
    259269      info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    260       info.commandPool = command_pool;
     270      info.commandPool = resourceCommandPool;
    261271      info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    262272      info.commandBufferCount = 1;
     
    288298
    289299      // This should make the command pool reusable for later
    290       err = vkResetCommandPool(this->device, command_pool, 0);
     300      err = vkResetCommandPool(this->device, resourceCommandPool, 0);
    291301      check_vk_result(err);
    292302   }
     
    918928
    919929      // Copy the UI image to a vulkan texture
     930      // TODO: I'm pretty sure this severely slows down the pipeline since this functions waits for the copy to be
     931      // complete before continuing. See if I can find a more efficient method.
    920932      VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer,
    921933         sdlOverlayImage, graphicsQueue);
     
    923935      renderScene();
    924936   }
    925 
    926    // TODO: Should probably check the returned result
    927    vkDeviceWaitIdle(device);
    928937}
    929938
     
    931940// which are already handled by updateObject(). Move this code to a different place,
    932941// where it will run just once per frame
    933 void VulkanGame::updateScene(uint32_t currentImage) {
     942void VulkanGame::updateScene() {
    934943   for (SceneObject<ModelVertex, SSBO_ModelObject>& model : this->modelObjects) {
    935944      model.model_transform =
     
    11101119   explosion_UBO.cur_time = curTime;
    11111120
    1112    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats);
    1113 
    1114    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], 0, ship_VP_mats);
    1115 
    1116    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], 0, asteroid_VP_mats);
    1117 
    1118    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats);
    1119 
    1120    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO);
     1121   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[imageIndex], 0, object_VP_mats);
     1122
     1123   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[imageIndex], 0, ship_VP_mats);
     1124
     1125   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[imageIndex], 0, asteroid_VP_mats);
     1126
     1127   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[imageIndex], 0, laser_VP_mats);
     1128
     1129   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[imageIndex], 0, explosion_UBO);
    11211130}
    11221131
    11231132// TODO: Maybe move all/most of this to the base Screen class
    11241133void VulkanGame::renderScene() {
    1125    vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
    1126 
    1127    uint32_t imageIndex;
    1128 
    11291134   // TODO: Recreate the swap chain here if the user went to a new screen
    11301135
    11311136   VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
    1132       imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
     1137      imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
    11331138
    11341139   if (result == VK_ERROR_OUT_OF_DATE_KHR) {
     
    11391144   }
    11401145
    1141    // TODO: Figure out a more elegant way to only do updates and render the UI once per scene render
    1142    // Probably move some of the renderScene() code into a higher function that updates the UI, and renders
    1143    // the UI and scene
    1144    updateScene(imageIndex);
     1146   if (vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()) != VK_SUCCESS) {
     1147      throw runtime_error("failed waiting for fence!");
     1148   }
     1149   if (vkResetFences(device, 1, &inFlightFences[imageIndex]) != VK_SUCCESS) {
     1150      throw runtime_error("failed to reset fence!");
     1151   }
     1152
     1153   updateScene();
     1154
     1155   VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] };
     1156   VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
     1157   VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] };
    11451158
    11461159   VkSubmitInfo submitInfo = {};
    11471160   submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    1148 
    1149    VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
    1150    VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
    1151 
    11521161   submitInfo.waitSemaphoreCount = 1;
    11531162   submitInfo.pWaitSemaphores = waitSemaphores;
     
    11551164   submitInfo.commandBufferCount = 1;
    11561165   submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
    1157 
    1158    VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
    1159 
    11601166   submitInfo.signalSemaphoreCount = 1;
    11611167   submitInfo.pSignalSemaphores = signalSemaphores;
    11621168
    1163    vkResetFences(device, 1, &inFlightFences[currentFrame]);
    1164 
    1165    if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
     1169   if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) {
    11661170      throw runtime_error("failed to submit draw command buffer!");
    11671171   }
     
    11711175   presentInfo.waitSemaphoreCount = 1;
    11721176   presentInfo.pWaitSemaphores = signalSemaphores;
    1173 
    1174    VkSwapchainKHR swapChains[] = { swapChain };
    11751177   presentInfo.swapchainCount = 1;
    1176    presentInfo.pSwapchains = swapChains;
     1178   presentInfo.pSwapchains = &swapChain;
    11771179   presentInfo.pImageIndices = &imageIndex;
    11781180   presentInfo.pResults = nullptr;
     
    11871189   }
    11881190
    1189    currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    1190    currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     1191   currentFrame = (currentFrame + 1) % swapChainImageCount;
    11911192}
    11921193
    11931194void VulkanGame::cleanup() {
     1195   // TODO: Should probably check the returned result
     1196   vkDeviceWaitIdle(device);
     1197
    11941198   ImGui_ImplVulkan_Shutdown();
    11951199   ImGui_ImplSDL2_Shutdown();
    11961200   ImGui::DestroyContext();
    11971201
     1202   // TODO: Probably move this into cleanupSwapChain once I finish the integration
    11981203   destroyImguiDescriptorPool();
    11991204
     
    12131218   explosionPipeline.cleanupBuffers();
    12141219
    1215    for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
    1216       vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
    1217       vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
    1218       vkDestroyFence(device, inFlightFences[i], nullptr);
    1219    }
    1220 
     1220   vkDestroyCommandPool(device, resourceCommandPool, nullptr);
    12211221   vkDestroyCommandPool(device, commandPool, nullptr);
     1222
    12221223   vkDestroyDevice(device, nullptr);
    12231224   vkDestroySurfaceKHR(instance, surface, nullptr);
     
    13791380
    13801381   if (extensionsSupported) {
    1381       SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
    1382       swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
     1382      vector<VkSurfaceFormatKHR> formats = VulkanUtils::querySwapChainFormats(physicalDevice, surface);
     1383      vector<VkPresentModeKHR> presentModes = VulkanUtils::querySwapChainPresentModes(physicalDevice, surface);
     1384
     1385      swapChainAdequate = !formats.empty() && !presentModes.empty();
    13831386   }
    13841387
     
    14451448}
    14461449
     1450void VulkanGame::chooseSwapChainProperties() {
     1451   vector<VkSurfaceFormatKHR> availableFormats = VulkanUtils::querySwapChainFormats(physicalDevice, surface);
     1452   vector<VkPresentModeKHR> availablePresentModes = VulkanUtils::querySwapChainPresentModes(physicalDevice, surface);
     1453
     1454   swapChainSurfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(availableFormats,
     1455      { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM },
     1456      VK_COLOR_SPACE_SRGB_NONLINEAR_KHR);
     1457
     1458   vector<VkPresentModeKHR> presentModes{
     1459      VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR
     1460   };
     1461   //vector<VkPresentModeKHR> presentModes{ VK_PRESENT_MODE_FIFO_KHR };
     1462
     1463   swapChainPresentMode = VulkanUtils::chooseSwapPresentMode(availablePresentModes, presentModes);
     1464
     1465   cout << "[vulkan] Selected PresentMode = " << swapChainPresentMode << endl;
     1466
     1467   VkSurfaceCapabilitiesKHR capabilities = VulkanUtils::querySwapChainCapabilities(physicalDevice, surface);
     1468
     1469   if (swapChainPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
     1470      swapChainMinImageCount = 3;
     1471   } else if (swapChainPresentMode == VK_PRESENT_MODE_FIFO_KHR || swapChainPresentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) {
     1472      swapChainMinImageCount = 2;
     1473   } else if (swapChainPresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
     1474      swapChainMinImageCount = 1;
     1475   } else {
     1476      throw runtime_error("unexpected present mode!");
     1477   }
     1478
     1479  if (swapChainMinImageCount < capabilities.minImageCount) {
     1480      swapChainMinImageCount = capabilities.minImageCount;
     1481   } else if (capabilities.maxImageCount != 0 && swapChainMinImageCount > capabilities.maxImageCount) {
     1482      swapChainMinImageCount = capabilities.maxImageCount;
     1483   }
     1484}
     1485
    14471486void VulkanGame::createSwapChain() {
    1448    SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
    1449 
    1450    VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
    1451    VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
    1452    VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
    1453 
    1454    swapChainImageCount = swapChainSupport.capabilities.minImageCount + 1;
    1455    if (swapChainSupport.capabilities.maxImageCount > 0 && swapChainImageCount > swapChainSupport.capabilities.maxImageCount) {
    1456       swapChainImageCount = swapChainSupport.capabilities.maxImageCount;
    1457    }
     1487   VkSurfaceCapabilitiesKHR capabilities = VulkanUtils::querySwapChainCapabilities(physicalDevice, surface);
     1488
     1489   swapChainExtent = VulkanUtils::chooseSwapExtent(capabilities, gui->getWindowWidth(), gui->getWindowHeight());
    14581490
    14591491   VkSwapchainCreateInfoKHR createInfo = {};
    14601492   createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    14611493   createInfo.surface = surface;
    1462    createInfo.minImageCount = swapChainImageCount;
    1463    createInfo.imageFormat = surfaceFormat.format;
    1464    createInfo.imageColorSpace = surfaceFormat.colorSpace;
    1465    createInfo.imageExtent = extent;
     1494   createInfo.minImageCount = swapChainMinImageCount;
     1495   createInfo.imageFormat = swapChainSurfaceFormat.format;
     1496   createInfo.imageColorSpace = swapChainSurfaceFormat.colorSpace;
     1497   createInfo.imageExtent = swapChainExtent;
    14661498   createInfo.imageArrayLayers = 1;
    14671499   createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    14681500
     1501   // TODO: Maybe save this result so I don't have to recalculate it every time
    14691502   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    14701503   uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
     
    14801513   }
    14811514
    1482    createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
     1515   createInfo.preTransform = capabilities.currentTransform;
    14831516   createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    1484    createInfo.presentMode = presentMode;
     1517   createInfo.presentMode = swapChainPresentMode;
    14851518   createInfo.clipped = VK_TRUE;
    14861519   createInfo.oldSwapchain = VK_NULL_HANDLE;
     
    14901523   }
    14911524
    1492    vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr);
     1525   if (vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr) != VK_SUCCESS) {
     1526      throw runtime_error("failed to get swap chain image count!");
     1527   }
     1528
    14931529   swapChainImages.resize(swapChainImageCount);
    1494    vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data());
    1495 
    1496    swapChainImageFormat = surfaceFormat.format;
    1497    swapChainExtent = extent;
     1530   if (vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data()) != VK_SUCCESS) {
     1531      throw runtime_error("failed to get swap chain images!");
     1532   }
    14981533}
    14991534
    15001535void VulkanGame::createImageViews() {
    1501    swapChainImageViews.resize(swapChainImages.size());
    1502 
    1503    for (size_t i = 0; i < swapChainImages.size(); i++) {
    1504       swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainImageFormat,
     1536   swapChainImageViews.resize(swapChainImageCount);
     1537
     1538   for (size_t i = 0; i < swapChainImageCount; i++) {
     1539      swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainSurfaceFormat.format,
    15051540         VK_IMAGE_ASPECT_COLOR_BIT);
    15061541   }
     
    15091544void VulkanGame::createRenderPass() {
    15101545   VkAttachmentDescription colorAttachment = {};
    1511    colorAttachment.format = swapChainImageFormat;
     1546   colorAttachment.format = swapChainSurfaceFormat.format;
    15121547   colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
    15131548   colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
     
    15741609}
    15751610
     1611void VulkanGame::createResourceCommandPool() {
     1612   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
     1613
     1614   VkCommandPoolCreateInfo poolInfo = {};
     1615   poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
     1616   poolInfo.queueFamilyIndex = indices.graphicsFamily.value();
     1617   poolInfo.flags = 0;
     1618
     1619   if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) {
     1620      throw runtime_error("failed to create resource command pool!");
     1621   }
     1622}
     1623
    15761624void VulkanGame::createCommandPool() {
    1577    QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);;
     1625   QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    15781626
    15791627   VkCommandPoolCreateInfo poolInfo = {};
     
    15881636
    15891637void VulkanGame::createImageResources() {
    1590    VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
     1638   VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,
    15911639      depthImage, graphicsQueue);
    15921640
     
    16021650   sdlOverlayImageDescriptor.sampler = textureSampler;
    16031651
    1604    VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
     1652   VulkanUtils::createVulkanImageFromFile(device, physicalDevice, resourceCommandPool, "textures/texture.jpg",
    16051653      floorTextureImage, graphicsQueue);
    16061654
     
    16101658   floorTextureImageDescriptor.sampler = textureSampler;
    16111659
    1612    VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/laser.png",
     1660   VulkanUtils::createVulkanImageFromFile(device, physicalDevice, resourceCommandPool, "textures/laser.png",
    16131661      laserTextureImage, graphicsQueue);
    16141662
     
    16461694
    16471695void VulkanGame::createFramebuffers() {
    1648    swapChainFramebuffers.resize(swapChainImageViews.size());
    1649 
    1650    for (size_t i = 0; i < swapChainImageViews.size(); i++) {
     1696   swapChainFramebuffers.resize(swapChainImageCount);
     1697
     1698   VkFramebufferCreateInfo framebufferInfo = {};
     1699   framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
     1700   framebufferInfo.renderPass = renderPass;
     1701   framebufferInfo.width = swapChainExtent.width;
     1702   framebufferInfo.height = swapChainExtent.height;
     1703   framebufferInfo.layers = 1;
     1704
     1705   for (uint32_t i = 0; i < swapChainImageCount; i++) {
    16511706      array<VkImageView, 2> attachments = {
    16521707         swapChainImageViews[i],
     
    16541709      };
    16551710
    1656       VkFramebufferCreateInfo framebufferInfo = {};
    1657       framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    1658       framebufferInfo.renderPass = renderPass;
    16591711      framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
    16601712      framebufferInfo.pAttachments = attachments.data();
    1661       framebufferInfo.width = swapChainExtent.width;
    1662       framebufferInfo.height = swapChainExtent.height;
    1663       framebufferInfo.layers = 1;
    16641713
    16651714      if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
     
    16701719
    16711720void VulkanGame::createCommandBuffers() {
    1672    commandBuffers.resize(swapChainImages.size());
     1721   commandBuffers.resize(swapChainImageCount);
    16731722
    16741723   VkCommandBufferAllocateInfo allocInfo = {};
     
    17451794}
    17461795
     1796void VulkanGame::createSyncObjects() {
     1797   imageAcquiredSemaphores.resize(swapChainImageCount);
     1798   renderCompleteSemaphores.resize(swapChainImageCount);
     1799   inFlightFences.resize(swapChainImageCount);
     1800
     1801   VkSemaphoreCreateInfo semaphoreInfo = {};
     1802   semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
     1803
     1804   VkFenceCreateInfo fenceInfo = {};
     1805   fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
     1806   fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
     1807
     1808   for (size_t i = 0; i < swapChainImageCount; i++) {
     1809      if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]) != VK_SUCCESS) {
     1810         throw runtime_error("failed to create image acquired sempahore for a frame!");
     1811      }
     1812
     1813      if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]) != VK_SUCCESS) {
     1814         throw runtime_error("failed to create render complete sempahore for a frame!");
     1815      }
     1816
     1817      if (vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
     1818         throw runtime_error("failed to create fence for a frame!");
     1819      }
     1820   }
     1821}
     1822
    17471823void VulkanGame::createImguiDescriptorPool() {
    17481824   vector<VkDescriptorPoolSize> pool_sizes{
     
    17731849void VulkanGame::destroyImguiDescriptorPool() {
    17741850   vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr);
    1775 }
    1776 
    1777 void VulkanGame::createSyncObjects() {
    1778    imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
    1779    renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
    1780    inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
    1781 
    1782    VkSemaphoreCreateInfo semaphoreInfo = {};
    1783    semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
    1784 
    1785    VkFenceCreateInfo fenceInfo = {};
    1786    fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    1787    fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
    1788 
    1789    for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
    1790       if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
    1791             vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
    1792             vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
    1793          throw runtime_error("failed to create synchronization objects for a frame!");
    1794       }
    1795    }
    17961851}
    17971852
     
    20022057void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
    20032058      vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) {
    2004    buffers.resize(swapChainImages.size());
    2005    buffersMemory.resize(swapChainImages.size());
    2006    bufferInfoList.resize(swapChainImages.size());
    2007 
    2008    for (size_t i = 0; i < swapChainImages.size(); i++) {
     2059   buffers.resize(swapChainImageCount);
     2060   buffersMemory.resize(swapChainImageCount);
     2061   bufferInfoList.resize(swapChainImageCount);
     2062
     2063   for (size_t i = 0; i < swapChainImageCount; i++) {
    20092064      VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags,
    20102065         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    20732128   createRenderPass();
    20742129
    2075    VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
     2130   VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,
    20762131      depthImage, graphicsQueue);
    20772132   createFramebuffers();
     
    21252180
    21262181   createCommandBuffers();
     2182
     2183   createSyncObjects();
     2184
     2185   imageIndex = 0;
    21272186}
    21282187
     
    21682227   }
    21692228
     2229   for (size_t i = 0; i < swapChainImageCount; i++) {
     2230      vkDestroySemaphore(device, imageAcquiredSemaphores[i], nullptr);
     2231      vkDestroySemaphore(device, renderCompleteSemaphores[i], nullptr);
     2232      vkDestroyFence(device, inFlightFences[i], nullptr);
     2233   }
     2234
    21702235   vkDestroyRenderPass(device, renderPass, nullptr);
    21712236
  • vulkan-game.hpp

    r7f60b28 r3f32dfd  
    198198class VulkanGame {
    199199   public:
    200       VulkanGame(int maxFramesInFlight);
     200      VulkanGame();
    201201      ~VulkanGame();
    202202
     
    234234         void* pUserData);
    235235
    236       // TODO: Make these consts static
    237       // Also, maybe move them into consts.hpp
    238 
    239       const int MAX_FRAMES_IN_FLIGHT;
    240 
    241236      const float NEAR_CLIP = 0.1f;
    242237      const float FAR_CLIP = 100.0f;
     
    268263      VkQueue presentQueue;
    269264
     265      // TODO: Maybe make a swapchain struct for convenience
     266      VkSurfaceFormatKHR swapChainSurfaceFormat;
     267      VkPresentModeKHR swapChainPresentMode;
     268      VkExtent2D swapChainExtent;
     269      uint32_t swapChainMinImageCount;
    270270      uint32_t swapChainImageCount;
    271271      VkSwapchainKHR swapChain;
    272272      vector<VkImage> swapChainImages;
    273       VkFormat swapChainImageFormat;
    274       VkExtent2D swapChainExtent;
    275273      vector<VkImageView> swapChainImageViews;
    276274      vector<VkFramebuffer> swapChainFramebuffers;
    277275
    278276      VkRenderPass renderPass;
     277
     278      VkCommandPool resourceCommandPool;
     279
    279280      VkCommandPool commandPool;
     281      vector<VkCommandPool> commandPools; // This is not used yet, but will be once
    280282      vector<VkCommandBuffer> commandBuffers;
    281283
    282284      VulkanImage depthImage;
    283285
    284       vector<VkSemaphore> imageAvailableSemaphores;
    285       vector<VkSemaphore> renderFinishedSemaphores;
     286      // These are per frame
     287      vector<VkSemaphore> imageAcquiredSemaphores;
     288      vector<VkSemaphore> renderCompleteSemaphores;
     289
     290      // These are per swap chain image
    286291      vector<VkFence> inFlightFences;
    287292
    288       size_t currentFrame;
     293      uint32_t imageIndex;
     294      uint32_t currentFrame;
    289295
    290296      bool framebufferResized;
     
    380386      void initMatrices();
    381387      void mainLoop();
    382       void updateScene(uint32_t currentImage);
     388      void updateScene();
    383389      void renderScene();
    384390      void cleanup();
     
    392398      void createLogicalDevice(const vector<const char*>& validationLayers,
    393399         const vector<const char*>& deviceExtensions);
     400      void chooseSwapChainProperties();
    394401      void createSwapChain();
    395402      void createImageViews();
    396403      void createRenderPass();
    397       VkFormat findDepthFormat();
     404      VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
     405      void createResourceCommandPool();
    398406      void createCommandPool();
    399407      void createImageResources();
    400 
    401       void createTextureSampler();
    402408      void createFramebuffers();
    403409      void createCommandBuffers();
    404410      void createSyncObjects();
     411
     412      void createTextureSampler();
    405413
    406414      void createImguiDescriptorPool();
Note: See TracChangeset for help on using the changeset viewer.