Changeset 4e2c709 in opengl-game for vulkan-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
-
vulkan-game.cpp
r9c0a614 r4e2c709 942 942 sdlOverlayImage, graphicsQueue); 943 943 944 renderScene(); 944 renderFrame(); 945 presentFrame(); 945 946 } 946 947 } … … 1139 1140 } 1140 1141 1141 // TODO: Maybe move all/most of this to the base Screen class1142 void VulkanGame::renderScene() {1143 // TODO: Recreate the swap chain here if the user went to a new screen1144 1145 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),1146 imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);1147 1148 if (result == VK_ERROR_OUT_OF_DATE_KHR) {1149 recreateSwapChain();1150 return;1151 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {1152 throw runtime_error("failed to acquire swap chain image!");1153 }1154 1155 if (vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()) != VK_SUCCESS) {1156 throw runtime_error("failed waiting for fence!");1157 }1158 if (vkResetFences(device, 1, &inFlightFences[imageIndex]) != VK_SUCCESS) {1159 throw runtime_error("failed to reset fence!");1160 }1161 1162 updateScene();1163 1164 VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] };1165 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };1166 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] };1167 1168 VkSubmitInfo submitInfo = {};1169 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;1170 submitInfo.waitSemaphoreCount = 1;1171 submitInfo.pWaitSemaphores = waitSemaphores;1172 submitInfo.pWaitDstStageMask = waitStages;1173 submitInfo.commandBufferCount = 1;1174 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];1175 submitInfo.signalSemaphoreCount = 1;1176 submitInfo.pSignalSemaphores = signalSemaphores;1177 1178 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) {1179 throw runtime_error("failed to submit draw command buffer!");1180 }1181 1182 VkPresentInfoKHR presentInfo = {};1183 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;1184 presentInfo.waitSemaphoreCount = 1;1185 presentInfo.pWaitSemaphores = signalSemaphores;1186 presentInfo.swapchainCount = 1;1187 presentInfo.pSwapchains = &swapChain;1188 presentInfo.pImageIndices = &imageIndex;1189 presentInfo.pResults = nullptr;1190 1191 result = vkQueuePresentKHR(presentQueue, &presentInfo);1192 1193 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {1194 framebufferResized = false;1195 recreateSwapChain();1196 } else if (result != VK_SUCCESS) {1197 throw runtime_error("failed to present swap chain image!");1198 }1199 1200 currentFrame = (currentFrame + 1) % swapChainImageCount;1201 }1202 1203 1142 void VulkanGame::cleanup() { 1204 1143 if (vkDeviceWaitIdle(device) != VK_SUCCESS) { … … 1806 1745 } 1807 1746 } 1747 } 1748 1749 void VulkanGame::renderFrame() { 1750 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), 1751 imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 1752 1753 if (result == VK_ERROR_OUT_OF_DATE_KHR) { 1754 recreateSwapChain(); 1755 return; 1756 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { 1757 throw runtime_error("failed to acquire swap chain image!"); 1758 } 1759 1760 if (vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()) != VK_SUCCESS) { 1761 throw runtime_error("failed waiting for fence!"); 1762 } 1763 if (vkResetFences(device, 1, &inFlightFences[imageIndex]) != VK_SUCCESS) { 1764 throw runtime_error("failed to reset fence!"); 1765 } 1766 1767 updateScene(); 1768 1769 VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] }; 1770 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; 1771 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] }; 1772 1773 VkSubmitInfo submitInfo = {}; 1774 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 1775 submitInfo.waitSemaphoreCount = 1; 1776 submitInfo.pWaitSemaphores = waitSemaphores; 1777 submitInfo.pWaitDstStageMask = waitStages; 1778 submitInfo.commandBufferCount = 1; 1779 submitInfo.pCommandBuffers = &commandBuffers[imageIndex]; 1780 submitInfo.signalSemaphoreCount = 1; 1781 submitInfo.pSignalSemaphores = signalSemaphores; 1782 1783 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) { 1784 throw runtime_error("failed to submit draw command buffer!"); 1785 } 1786 } 1787 1788 void VulkanGame::presentFrame() { 1789 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] }; 1790 1791 VkPresentInfoKHR presentInfo = {}; 1792 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 1793 presentInfo.waitSemaphoreCount = 1; 1794 presentInfo.pWaitSemaphores = signalSemaphores; 1795 presentInfo.swapchainCount = 1; 1796 presentInfo.pSwapchains = &swapChain; 1797 presentInfo.pImageIndices = &imageIndex; 1798 presentInfo.pResults = nullptr; 1799 1800 VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo); 1801 1802 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) { 1803 framebufferResized = false; 1804 recreateSwapChain(); 1805 } else if (result != VK_SUCCESS) { 1806 throw runtime_error("failed to present swap chain image!"); 1807 } 1808 1809 currentFrame = (currentFrame + 1) % swapChainImageCount; 1808 1810 } 1809 1811
Note:
See TracChangeset
for help on using the changeset viewer.