Changeset 47bff4c in opengl-game
- Timestamp:
- Jul 19, 2019, 3:03:12 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 75108ef
- Parents:
- ebeb3aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rebeb3aa r47bff4c 28 28 const int SCREEN_HEIGHT = 600; 29 29 30 const int MAX_FRAMES_IN_FLIGHT = 2; 31 30 32 #ifdef NDEBUG 31 33 const bool enableValidationLayers = false; … … 116 118 VkPipelineLayout pipelineLayout; 117 119 VkPipeline graphicsPipeline; 120 VkCommandPool commandPool; 118 121 119 122 vector<VkFramebuffer> swapChainFramebuffers; 123 vector<VkCommandBuffer> commandBuffers; 124 125 vector<VkSemaphore> imageAvailableSemaphores; 126 vector<VkSemaphore> renderFinishedSemaphores; 127 vector<VkFence> inFlightFences; 128 129 size_t currentFrame = 0; 120 130 121 131 // both SDL and GLFW create window functions return NULL on failure … … 154 164 createGraphicsPipeline(); 155 165 createFramebuffers(); 166 createCommandPool(); 167 createCommandBuffers(); 168 createSyncObjects(); 156 169 } 157 170 … … 585 598 subpass.pColorAttachments = &colorAttachmentRef; 586 599 600 VkSubpassDependency dependency = {}; 601 dependency.srcSubpass = VK_SUBPASS_EXTERNAL; 602 dependency.dstSubpass = 0; 603 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 604 dependency.srcAccessMask = 0; 605 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 606 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; 607 587 608 VkRenderPassCreateInfo renderPassInfo = {}; 588 609 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; … … 591 612 renderPassInfo.subpassCount = 1; 592 613 renderPassInfo.pSubpasses = &subpass; 614 renderPassInfo.dependencyCount = 1; 615 renderPassInfo.pDependencies = &dependency; 593 616 594 617 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) { … … 749 772 } 750 773 774 void createCommandPool() { 775 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice); 776 777 VkCommandPoolCreateInfo poolInfo = {}; 778 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; 779 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value(); 780 poolInfo.flags = 0; 781 782 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) { 783 throw runtime_error("failed to create command pool!"); 784 } 785 } 786 787 void createCommandBuffers() { 788 commandBuffers.resize(swapChainFramebuffers.size()); 789 790 VkCommandBufferAllocateInfo allocInfo = {}; 791 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; 792 allocInfo.commandPool = commandPool; 793 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 794 allocInfo.commandBufferCount = (uint32_t)commandBuffers.size(); 795 796 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) { 797 throw runtime_error("failed to create command buffers!"); 798 } 799 800 for (size_t i = 0; i < commandBuffers.size(); i++) { 801 VkCommandBufferBeginInfo beginInfo = {}; 802 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 803 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; 804 beginInfo.pInheritanceInfo = nullptr; 805 806 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) { 807 throw runtime_error("failed to begin recording command buffer!"); 808 } 809 810 VkRenderPassBeginInfo renderPassInfo = {}; 811 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; 812 renderPassInfo.renderPass = renderPass; 813 renderPassInfo.framebuffer = swapChainFramebuffers[i]; 814 renderPassInfo.renderArea.offset = { 0, 0 }; 815 renderPassInfo.renderArea.extent = swapChainExtent; 816 817 VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f }; 818 renderPassInfo.clearValueCount = 1; 819 renderPassInfo.pClearValues = &clearColor; 820 821 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 822 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); 823 vkCmdDraw(commandBuffers[i], 3, 1, 0, 0); 824 vkCmdEndRenderPass(commandBuffers[i]); 825 826 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) { 827 throw runtime_error("failed to record command buffer!"); 828 } 829 } 830 } 831 832 void createSyncObjects() { 833 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT); 834 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT); 835 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT); 836 837 VkSemaphoreCreateInfo semaphoreInfo = {}; 838 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; 839 840 VkFenceCreateInfo fenceInfo = {}; 841 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; 842 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; 843 844 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { 845 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS || 846 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS || 847 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) { 848 throw runtime_error("failed to create synchronization objects for a frame!"); 849 } 850 } 851 } 852 751 853 void mainLoop() { 752 854 // TODO: Create some generic event-handling functions in game-gui-* … … 765 867 quit = true; 766 868 } 767 768 /**/ 769 SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0xFF, 0xFF, 0xFF)); 770 771 SDL_UpdateWindowSurface(window); 772 /**/ 773 } 774 } 869 } 870 871 drawFrame(); 872 873 //SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0x00, 0x99, 0x99)); 874 //SDL_UpdateWindowSurface(window); 875 } 876 877 vkDeviceWaitIdle(device); 878 } 879 880 void drawFrame() { 881 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max()); 882 vkResetFences(device, 1, &inFlightFences[currentFrame]); 883 884 uint32_t imageIndex; 885 886 vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 887 888 VkSubmitInfo submitInfo = {}; 889 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 890 891 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] }; 892 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; 893 894 submitInfo.waitSemaphoreCount = 1; 895 submitInfo.pWaitSemaphores = waitSemaphores; 896 submitInfo.pWaitDstStageMask = waitStages; 897 submitInfo.commandBufferCount = 1; 898 submitInfo.pCommandBuffers = &commandBuffers[imageIndex]; 899 900 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] }; 901 902 submitInfo.signalSemaphoreCount = 1; 903 submitInfo.pSignalSemaphores = signalSemaphores; 904 905 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) { 906 throw runtime_error("failed to submit draw command buffer!"); 907 } 908 909 VkPresentInfoKHR presentInfo = {}; 910 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 911 912 presentInfo.waitSemaphoreCount = 1; 913 presentInfo.pWaitSemaphores = signalSemaphores; 914 915 VkSwapchainKHR swapChains[] = { swapChain }; 916 presentInfo.swapchainCount = 1; 917 presentInfo.pSwapchains = swapChains; 918 presentInfo.pImageIndices = &imageIndex; 919 presentInfo.pResults = nullptr; 920 921 vkQueuePresentKHR(presentQueue, &presentInfo); 922 923 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 775 924 } 776 925 777 926 void cleanup() { 927 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { 928 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); 929 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr); 930 vkDestroyFence(device, inFlightFences[i], nullptr); 931 } 932 933 vkDestroyCommandPool(device, commandPool, nullptr); 934 778 935 for (auto framebuffer : swapChainFramebuffers) { 779 936 vkDestroyFramebuffer(device, framebuffer, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.