Changeset d2d9286 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Nov 7, 2019, 2:05:17 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
f985231
Parents:
87c8f1a
Message:

In vulkangame, implement the renderScene function to draw a frame in Vulkan and implement a test UI overlay

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r87c8f1a rd2d9286  
    215215   UIEvent e;
    216216   bool quit = false;
    217 
    218    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    219217
    220218   while (!quit) {
     
    266264
    267265void VulkanGame::renderUI() {
     266   // TODO: Since I currently don't use any other render targets,
     267   // I may as well set this once before the render loop
     268   SDL_SetRenderTarget(renderer, uiOverlay);
     269
     270   SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
    268271   SDL_RenderClear(renderer);
    269    SDL_RenderPresent(renderer);
     272
     273   SDL_Rect rect = {280, 220, 100, 100};
     274   SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
     275   SDL_RenderFillRect(renderer, &rect);
     276
     277   SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
     278   SDL_RenderDrawLine(renderer, 50, 5, 150, 500);
     279
     280   VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer,
     281      sdlOverlayImage, graphicsQueue);
    270282}
    271283
     
    274286
    275287   uint32_t imageIndex;
     288
     289   VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
     290      imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
     291
     292   if (result == VK_ERROR_OUT_OF_DATE_KHR) {
     293      recreateSwapChain();
     294      return;
     295   } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
     296      throw runtime_error("failed to acquire swap chain image!");
     297   }
     298
     299   VkSubmitInfo submitInfo = {};
     300   submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
     301
     302   VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
     303   VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
     304
     305   submitInfo.waitSemaphoreCount = 1;
     306   submitInfo.pWaitSemaphores = waitSemaphores;
     307   submitInfo.pWaitDstStageMask = waitStages;
     308   submitInfo.commandBufferCount = 1;
     309   submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
     310
     311   VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
     312
     313   submitInfo.signalSemaphoreCount = 1;
     314   submitInfo.pSignalSemaphores = signalSemaphores;
     315
     316   vkResetFences(device, 1, &inFlightFences[currentFrame]);
     317
     318   if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
     319      throw runtime_error("failed to submit draw command buffer!");
     320   }
     321
     322   VkPresentInfoKHR presentInfo = {};
     323   presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
     324   presentInfo.waitSemaphoreCount = 1;
     325   presentInfo.pWaitSemaphores = signalSemaphores;
     326
     327   VkSwapchainKHR swapChains[] = { swapChain };
     328   presentInfo.swapchainCount = 1;
     329   presentInfo.pSwapchains = swapChains;
     330   presentInfo.pImageIndices = &imageIndex;
     331   presentInfo.pResults = nullptr;
     332
     333   result = vkQueuePresentKHR(presentQueue, &presentInfo);
     334
     335   if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
     336      framebufferResized = false;
     337      recreateSwapChain();
     338   } else if (result != VK_SUCCESS) {
     339      throw runtime_error("failed to present swap chain image!");
     340   }
    276341
    277342   currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     
    774839      vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    775840
    776       // reateGraphicsPipelineCommands(scenePipeline, i);
    777       // createGraphicsPipelineCommands(overlayPipeline, i);
    778841      for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
    779842         pipeline.createRenderCommands(commandBuffers[i], i);
     
    809872}
    810873
     874void VulkanGame::recreateSwapChain() {
     875   cout << "Recreating swap chain" << endl;
     876   gui->refreshWindowSize();
     877
     878   while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
     879      (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
     880      SDL_WaitEvent(nullptr);
     881      gui->refreshWindowSize();
     882   }
     883
     884   vkDeviceWaitIdle(device);
     885
     886   //cleanupSwapChain();
     887}
     888
    811889void VulkanGame::cleanupSwapChain() {
    812890   VulkanUtils::destroyVulkanImage(device, depthImage);
Note: See TracChangeset for help on using the changeset viewer.