Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. void drawFrame()
  2.     {
  3.         vkWaitForFences(m_Device, 1, &m_inFlightFences[m_CurrentFrame], VK_TRUE, UINT64_MAX);
  4.  
  5.         uint32_t imageIndex;
  6.         VkResult result = vkAcquireNextImageKHR(m_Device, m_SwapChain, UINT64_MAX, m_ImageAvailableSemaphore[m_CurrentFrame],
  7.             VK_NULL_HANDLE, &imageIndex);
  8.  
  9.         if (result == VK_ERROR_OUT_OF_DATE_KHR)
  10.         {
  11.             recreateSwapChain();
  12.             return;
  13.         }
  14.         else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
  15.         {
  16.             throw std::runtime_error("Failed to acquire swap chain image!");
  17.         }
  18.  
  19.         updateUniformBuffer(imageIndex);
  20.  
  21.         // Check if a previous frame is using this image (i.e. there is its fence to wait on)
  22.         if (m_ImagesInFlight[imageIndex] != VK_NULL_HANDLE)
  23.         {
  24.             vkWaitForFences(m_Device, 1, &m_ImagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
  25.         }
  26.  
  27.         // Mark the image as now being in use by this frame
  28.         m_ImagesInFlight[imageIndex] = m_inFlightFences[m_CurrentFrame];
  29.  
  30.         VkSubmitInfo submitInfo = {};
  31.         submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  32.  
  33.         VkSemaphore waitSemaphores[] = { m_ImageAvailableSemaphore[m_CurrentFrame] };
  34.         VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
  35.         submitInfo.waitSemaphoreCount = 1;
  36.         submitInfo.pWaitSemaphores = waitSemaphores;
  37.         submitInfo.pWaitDstStageMask = waitStages;
  38.         submitInfo.commandBufferCount = 1;
  39.         submitInfo.pCommandBuffers = &m_CommandBuffers[imageIndex];
  40.  
  41.         VkSemaphore signalSemaphores[] = { m_RenderFinishedSemaphore[m_CurrentFrame] };
  42.         submitInfo.signalSemaphoreCount = 1;
  43.         submitInfo.pSignalSemaphores = signalSemaphores;
  44.  
  45.         vkResetFences(m_Device, 1, &m_inFlightFences[m_CurrentFrame]);
  46.  
  47.         if (vkQueueSubmit(m_GraphicsQueue, 1, &submitInfo, m_inFlightFences[m_CurrentFrame]) != VK_SUCCESS)
  48.         {
  49.             throw std::runtime_error("Failed to submit draw command buffer!");
  50.         }
  51.  
  52.         VkPresentInfoKHR presentInfo = {};
  53.         presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  54.         presentInfo.waitSemaphoreCount = 1;
  55.         presentInfo.pWaitSemaphores = signalSemaphores;
  56.  
  57.         VkSwapchainKHR swapChains[] = { m_SwapChain };
  58.         presentInfo.swapchainCount = 1;
  59.         presentInfo.pSwapchains = swapChains;
  60.         presentInfo.pImageIndices = &imageIndex;
  61.         presentInfo.pResults = nullptr; // Optional
  62.  
  63.         result = vkQueuePresentKHR(m_PresentQueue, &presentInfo);
  64.  
  65.         if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || m_FrameBufferResized)
  66.         {
  67.             m_FrameBufferResized = false;
  68.             recreateSwapChain();
  69.         }
  70.         else if (result != VK_SUCCESS)
  71.         {
  72.             throw std::runtime_error("Failed to present swap chain image!");
  73.         }
  74.  
  75.         m_CurrentFrame = (m_CurrentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement