Changeset 4e2c709 in opengl-game for sdl-game.cpp
- Timestamp:
- Feb 14, 2021, 2:44:21 AM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 28ea92f
- Parents:
- 9c0a614
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sdl-game.cpp
r9c0a614 r4e2c709 31 31 } 32 32 } 33 34 void VulkanGame::FrameRender(ImDrawData* draw_data) {35 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),36 imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);37 38 if (result == VK_ERROR_OUT_OF_DATE_KHR) {39 g_SwapChainRebuild = true;40 return;41 } else {42 VKUTIL_CHECK_RESULT(result, "failed to acquire swap chain image!");43 }44 45 VKUTIL_CHECK_RESULT(vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()),46 "failed waiting for fence!");47 48 VKUTIL_CHECK_RESULT(vkResetFences(device, 1, &inFlightFences[imageIndex]),49 "failed to reset fence!");50 51 // START OF NEW CODE52 // I don't have analogous code in vulkan-game right now because I record command buffers once53 // before the render loop ever starts. I should change this54 55 VKUTIL_CHECK_RESULT(vkResetCommandPool(device, commandPools[imageIndex], 0),56 "failed to reset command pool!");57 58 VkCommandBufferBeginInfo info = {};59 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;60 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;61 62 VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers[imageIndex], &info),63 "failed to begin recording command buffer!");64 65 VkRenderPassBeginInfo renderPassInfo = {};66 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;67 renderPassInfo.renderPass = renderPass;68 renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex];69 renderPassInfo.renderArea.extent = swapChainExtent;70 71 array<VkClearValue, 2> clearValues = {};72 clearValues[0].color = { { 0.45f, 0.55f, 0.60f, 1.00f } };73 clearValues[1].depthStencil = { 1.0f, 0 };74 75 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());76 renderPassInfo.pClearValues = clearValues.data();77 78 vkCmdBeginRenderPass(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);79 80 // Record dear imgui primitives into command buffer81 ImGui_ImplVulkan_RenderDrawData(draw_data, commandBuffers[imageIndex]);82 83 // Submit command buffer84 vkCmdEndRenderPass(commandBuffers[imageIndex]);85 86 VKUTIL_CHECK_RESULT(vkEndCommandBuffer(commandBuffers[imageIndex]),87 "failed to record command buffer!");88 89 // END OF NEW CODE90 91 VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] };92 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;93 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] };94 95 VkSubmitInfo submitInfo = {};96 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;97 submitInfo.waitSemaphoreCount = 1;98 submitInfo.pWaitSemaphores = waitSemaphores;99 submitInfo.pWaitDstStageMask = &wait_stage;100 submitInfo.commandBufferCount = 1;101 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];102 submitInfo.signalSemaphoreCount = 1;103 submitInfo.pSignalSemaphores = signalSemaphores;104 105 VKUTIL_CHECK_RESULT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]),106 "failed to submit draw command buffer!");107 }108 109 void VulkanGame::FramePresent() {110 if (g_SwapChainRebuild)111 return;112 113 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] };114 115 VkPresentInfoKHR presentInfo = {};116 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;117 presentInfo.waitSemaphoreCount = 1;118 presentInfo.pWaitSemaphores = signalSemaphores;119 presentInfo.swapchainCount = 1;120 presentInfo.pSwapchains = &swapChain;121 presentInfo.pImageIndices = &imageIndex;122 presentInfo.pResults = nullptr;123 124 VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);125 126 // In vulkan-game, I also handle VK_SUBOPTIMAL_KHR and framebufferResized. g_SwapChainRebuild is kind of similar127 // to framebufferResized, but not quite the same128 if (result == VK_ERROR_OUT_OF_DATE_KHR) {129 g_SwapChainRebuild = true;130 return;131 } else if (result != VK_SUCCESS) {132 throw runtime_error("failed to present swap chain image!");133 }134 135 currentFrame = (currentFrame + 1) % swapChainImageCount;136 }137 138 /********************************************* START OF NEW CODE *********************************************/139 33 140 34 VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback( … … 337 231 const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); 338 232 if (!is_minimized) { 339 FrameRender(draw_data);340 FramePresent();233 renderFrame(draw_data); 234 presentFrame(); 341 235 } 342 236 } … … 924 818 } 925 819 820 void VulkanGame::renderFrame(ImDrawData* draw_data) { 821 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), 822 imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 823 824 if (result == VK_ERROR_OUT_OF_DATE_KHR) { 825 g_SwapChainRebuild = true; 826 return; 827 } 828 else { 829 VKUTIL_CHECK_RESULT(result, "failed to acquire swap chain image!"); 830 } 831 832 VKUTIL_CHECK_RESULT(vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()), 833 "failed waiting for fence!"); 834 835 VKUTIL_CHECK_RESULT(vkResetFences(device, 1, &inFlightFences[imageIndex]), 836 "failed to reset fence!"); 837 838 // START OF NEW CODE 839 // I don't have analogous code in vulkan-game right now because I record command buffers once 840 // before the render loop ever starts. I should change this 841 842 VKUTIL_CHECK_RESULT(vkResetCommandPool(device, commandPools[imageIndex], 0), 843 "failed to reset command pool!"); 844 845 VkCommandBufferBeginInfo info = {}; 846 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 847 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 848 849 VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers[imageIndex], &info), 850 "failed to begin recording command buffer!"); 851 852 VkRenderPassBeginInfo renderPassInfo = {}; 853 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; 854 renderPassInfo.renderPass = renderPass; 855 renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex]; 856 renderPassInfo.renderArea.extent = swapChainExtent; 857 858 array<VkClearValue, 2> clearValues = {}; 859 clearValues[0].color = { { 0.45f, 0.55f, 0.60f, 1.00f } }; 860 clearValues[1].depthStencil = { 1.0f, 0 }; 861 862 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size()); 863 renderPassInfo.pClearValues = clearValues.data(); 864 865 vkCmdBeginRenderPass(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 866 867 // Record dear imgui primitives into command buffer 868 ImGui_ImplVulkan_RenderDrawData(draw_data, commandBuffers[imageIndex]); 869 870 // Submit command buffer 871 vkCmdEndRenderPass(commandBuffers[imageIndex]); 872 873 VKUTIL_CHECK_RESULT(vkEndCommandBuffer(commandBuffers[imageIndex]), 874 "failed to record command buffer!"); 875 876 // END OF NEW CODE 877 878 VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] }; 879 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 880 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] }; 881 882 VkSubmitInfo submitInfo = {}; 883 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 884 submitInfo.waitSemaphoreCount = 1; 885 submitInfo.pWaitSemaphores = waitSemaphores; 886 submitInfo.pWaitDstStageMask = &wait_stage; 887 submitInfo.commandBufferCount = 1; 888 submitInfo.pCommandBuffers = &commandBuffers[imageIndex]; 889 submitInfo.signalSemaphoreCount = 1; 890 submitInfo.pSignalSemaphores = signalSemaphores; 891 892 VKUTIL_CHECK_RESULT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]), 893 "failed to submit draw command buffer!"); 894 } 895 896 void VulkanGame::presentFrame() { 897 if (g_SwapChainRebuild) 898 return; 899 900 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] }; 901 902 VkPresentInfoKHR presentInfo = {}; 903 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 904 presentInfo.waitSemaphoreCount = 1; 905 presentInfo.pWaitSemaphores = signalSemaphores; 906 presentInfo.swapchainCount = 1; 907 presentInfo.pSwapchains = &swapChain; 908 presentInfo.pImageIndices = &imageIndex; 909 presentInfo.pResults = nullptr; 910 911 VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo); 912 913 // In vulkan-game, I also handle VK_SUBOPTIMAL_KHR and framebufferResized. g_SwapChainRebuild is kind of similar 914 // to framebufferResized, but not quite the same 915 if (result == VK_ERROR_OUT_OF_DATE_KHR) { 916 g_SwapChainRebuild = true; 917 return; 918 } 919 else if (result != VK_SUCCESS) { 920 throw runtime_error("failed to present swap chain image!"); 921 } 922 923 currentFrame = (currentFrame + 1) % swapChainImageCount; 924 } 925 926 926 void VulkanGame::recreateSwapChain() { 927 927 if (vkDeviceWaitIdle(device) != VK_SUCCESS) { … … 977 977 vkDestroySwapchainKHR(device, swapChain, nullptr); 978 978 } 979 980 /********************************************** END OF NEW CODE **********************************************/
Note:
See TracChangeset
for help on using the changeset viewer.