Changeset e469aed in opengl-game
- Timestamp:
- Apr 7, 2021, 1:38:54 AM (3 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 5081b9a
- Parents:
- cefdf23
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sdl-game.cpp
rcefdf23 re469aed 60 60 , shouldRecreateSwapChain(false) 61 61 , frameCount(0) 62 , currentFrame( )62 , currentFrame(0) 63 63 , imageIndex(0) 64 64 , fpsStartTime(0.0f) … … 66 66 , done(false) 67 67 , currentRenderScreenFn(nullptr) 68 , descriptorPool(VK_NULL_HANDLE)68 , imguiDescriptorPool(VK_NULL_HANDLE) 69 69 , gui(nullptr) 70 70 , window(nullptr) … … 77 77 78 78 void VulkanGame::run(int width, int height, unsigned char guiFlags) { 79 // TODO: Maybe call the init code in the constructor instead of in run()80 // Research this81 79 cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl; 82 80 83 81 cout << "Vulkan Game" << endl; 84 82 85 // TODO: Move IMGUI initialization in here86 83 if (initUI(width, height, guiFlags) == RTWO_ERROR) { 87 84 return; … … 90 87 initVulkan(); 91 88 92 // Create Descriptor Pool 93 { 94 VkDescriptorPoolSize pool_sizes[] = 95 { 96 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, 97 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, 98 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, 99 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, 100 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, 101 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, 102 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, 103 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, 104 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, 105 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, 106 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } 107 }; 108 VkDescriptorPoolCreateInfo pool_info = {}; 109 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 110 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 111 pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes); 112 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes); 113 pool_info.pPoolSizes = pool_sizes; 114 115 VKUTIL_CHECK_RESULT(vkCreateDescriptorPool(device, &pool_info, nullptr, &descriptorPool), 116 "failed to create descriptor pool"); 117 } 118 119 // TODO: Do this in one place and save it instead of redoing it every time I need a queue family index 120 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface); 121 122 // Setup Dear ImGui context 123 IMGUI_CHECKVERSION(); 124 ImGui::CreateContext(); 89 initImGuiOverlay(); 90 91 currentRenderScreenFn = &VulkanGame::renderMainScreen; 92 125 93 ImGuiIO& io = ImGui::GetIO(); 126 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls127 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls128 129 // Setup Dear ImGui style130 ImGui::StyleColorsDark();131 //ImGui::StyleColorsClassic();132 133 // Setup Platform/Renderer bindings134 ImGui_ImplSDL2_InitForVulkan(window);135 ImGui_ImplVulkan_InitInfo init_info = {};136 init_info.Instance = instance;137 init_info.PhysicalDevice = physicalDevice;138 init_info.Device = device;139 init_info.QueueFamily = indices.graphicsFamily.value();140 init_info.Queue = graphicsQueue;141 init_info.DescriptorPool = descriptorPool;142 init_info.Allocator = nullptr;143 init_info.MinImageCount = swapChainMinImageCount;144 init_info.ImageCount = swapChainImageCount;145 init_info.CheckVkResultFn = check_imgui_vk_result;146 ImGui_ImplVulkan_Init(&init_info, renderPass);147 148 // Load Fonts149 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.150 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.151 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).152 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.153 // - Read 'docs/FONTS.md' for more instructions and details.154 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !155 //io.Fonts->AddFontDefault();156 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);157 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);158 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);159 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);160 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());161 //assert(font != NULL);162 163 // Upload Fonts164 {165 VkCommandBuffer commandBuffer = VulkanUtils::beginSingleTimeCommands(device, resourceCommandPool);166 167 ImGui_ImplVulkan_CreateFontsTexture(commandBuffer);168 169 VulkanUtils::endSingleTimeCommands(device, resourceCommandPool, commandBuffer, graphicsQueue);170 171 ImGui_ImplVulkan_DestroyFontUploadObjects();172 }173 174 currentRenderScreenFn = &VulkanGame::renderMainScreen;175 94 176 95 initGuiValueLists(valueLists); … … 221 140 cout << "UI library could not be initialized!" << endl; 222 141 cout << gui->getError() << endl; 142 // TODO: Rename RTWO_ERROR to something else 223 143 return RTWO_ERROR; 224 144 } … … 253 173 createSwapChain(); 254 174 createImageViews(); 175 176 createResourceCommandPool(); 177 createImageResources(); 178 255 179 createRenderPass(); 256 257 createResourceCommandPool();258 180 createCommandPools(); 259 260 VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent,261 depthImage, graphicsQueue);262 263 181 createFramebuffers(); 264 265 // TODO: Initialize pipelines here266 267 182 createCommandBuffers(); 268 269 183 createSyncObjects(); 270 184 } … … 354 268 } 355 269 356 // Start the Dear ImGui frame270 // TODO: Move this into a renderImGuiOverlay() function 357 271 ImGui_ImplVulkan_NewFrame(); 358 272 ImGui_ImplSDL2_NewFrame(window); … … 378 292 VKUTIL_CHECK_RESULT(vkDeviceWaitIdle(device), "failed to wait for device!"); 379 293 380 ImGui_ImplVulkan_Shutdown(); 381 ImGui_ImplSDL2_Shutdown(); 382 ImGui::DestroyContext(); 294 cleanupImGuiOverlay(); 383 295 384 296 cleanupSwapChain(); 385 386 // this would actually be destroyed in the pipeline class387 vkDestroyDescriptorPool(device, descriptorPool, nullptr);388 297 389 298 vkDestroyCommandPool(device, resourceCommandPool, nullptr); … … 702 611 VK_IMAGE_ASPECT_COLOR_BIT); 703 612 } 613 } 614 615 void VulkanGame::createResourceCommandPool() { 616 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface); 617 618 VkCommandPoolCreateInfo poolInfo = {}; 619 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; 620 poolInfo.queueFamilyIndex = indices.graphicsFamily.value(); 621 poolInfo.flags = 0; 622 623 if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) { 624 throw runtime_error("failed to create resource command pool!"); 625 } 626 } 627 628 void VulkanGame::createImageResources() { 629 VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent, 630 depthImage, graphicsQueue); 631 } 632 633 VkFormat VulkanGame::findDepthFormat() { 634 return VulkanUtils::findSupportedFormat( 635 physicalDevice, 636 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, 637 VK_IMAGE_TILING_OPTIMAL, 638 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT 639 ); 704 640 } 705 641 … … 766 702 } 767 703 768 VkFormat VulkanGame::findDepthFormat() {769 return VulkanUtils::findSupportedFormat(770 physicalDevice,771 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },772 VK_IMAGE_TILING_OPTIMAL,773 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT774 );775 }776 777 void VulkanGame::createResourceCommandPool() {778 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface);779 780 VkCommandPoolCreateInfo poolInfo = {};781 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;782 poolInfo.queueFamilyIndex = indices.graphicsFamily.value();783 poolInfo.flags = 0;784 785 if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) {786 throw runtime_error("failed to create resource command pool!");787 }788 }789 790 704 void VulkanGame::createCommandPools() { 791 705 commandPools.resize(swapChainImageCount); … … 859 773 860 774 for (size_t i = 0; i < swapChainImageCount; i++) { 861 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]) != VK_SUCCESS) { 862 throw runtime_error("failed to create image acquired sempahore for a frame!"); 863 } 864 865 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]) != VK_SUCCESS) { 866 throw runtime_error("failed to create render complete sempahore for a frame!"); 867 } 868 869 if (vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) { 870 throw runtime_error("failed to create fence for a frame!"); 871 } 872 } 775 VKUTIL_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]), 776 "failed to create image acquired sempahore for a frame!"); 777 778 VKUTIL_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]), 779 "failed to create render complete sempahore for a frame!"); 780 781 VKUTIL_CHECK_RESULT(vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]), 782 "failed to create fence for a frame!"); 783 } 784 } 785 786 void VulkanGame::initImGuiOverlay() { 787 vector<VkDescriptorPoolSize> pool_sizes { 788 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, 789 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, 790 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, 791 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, 792 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, 793 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, 794 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, 795 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, 796 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, 797 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, 798 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } 799 }; 800 801 VkDescriptorPoolCreateInfo pool_info = {}; 802 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 803 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 804 pool_info.maxSets = 1000 * pool_sizes.size(); 805 pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size()); 806 pool_info.pPoolSizes = pool_sizes.data(); 807 808 VKUTIL_CHECK_RESULT(vkCreateDescriptorPool(device, &pool_info, nullptr, &imguiDescriptorPool), 809 "failed to create IMGUI descriptor pool!"); 810 811 // TODO: Do this in one place and save it instead of redoing it every time I need a queue family index 812 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, vulkanSurface); 813 814 // Setup Dear ImGui context 815 IMGUI_CHECKVERSION(); 816 ImGui::CreateContext(); 817 ImGuiIO& io = ImGui::GetIO(); 818 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 819 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 820 821 // Setup Dear ImGui style 822 ImGui::StyleColorsDark(); 823 //ImGui::StyleColorsClassic(); 824 825 // Setup Platform/Renderer bindings 826 ImGui_ImplSDL2_InitForVulkan(window); 827 ImGui_ImplVulkan_InitInfo init_info = {}; 828 init_info.Instance = instance; 829 init_info.PhysicalDevice = physicalDevice; 830 init_info.Device = device; 831 init_info.QueueFamily = indices.graphicsFamily.value(); 832 init_info.Queue = graphicsQueue; 833 init_info.DescriptorPool = imguiDescriptorPool; 834 init_info.Allocator = nullptr; 835 init_info.MinImageCount = swapChainMinImageCount; 836 init_info.ImageCount = swapChainImageCount; 837 init_info.CheckVkResultFn = check_imgui_vk_result; 838 ImGui_ImplVulkan_Init(&init_info, renderPass); 839 840 // Load Fonts 841 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. 842 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 843 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). 844 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. 845 // - Read 'docs/FONTS.md' for more instructions and details. 846 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! 847 //io.Fonts->AddFontDefault(); 848 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); 849 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); 850 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); 851 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); 852 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); 853 //assert(font != NULL); 854 855 // Upload Fonts 856 857 VkCommandBuffer commandBuffer = VulkanUtils::beginSingleTimeCommands(device, resourceCommandPool); 858 859 ImGui_ImplVulkan_CreateFontsTexture(commandBuffer); 860 861 VulkanUtils::endSingleTimeCommands(device, resourceCommandPool, commandBuffer, graphicsQueue); 862 863 ImGui_ImplVulkan_DestroyFontUploadObjects(); 864 } 865 866 void VulkanGame::cleanupImGuiOverlay() { 867 ImGui_ImplVulkan_Shutdown(); 868 ImGui_ImplSDL2_Shutdown(); 869 ImGui::DestroyContext(); 870 871 vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr); 873 872 } 874 873 … … 898 897 VkCommandBufferBeginInfo beginInfo = {}; 899 898 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 900 beginInfo.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;899 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 901 900 902 901 VKUTIL_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers[imageIndex], &beginInfo), … … 907 906 renderPassInfo.renderPass = renderPass; 908 907 renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex]; 908 renderPassInfo.renderArea.offset = { 0, 0 }; 909 909 renderPassInfo.renderArea.extent = swapChainExtent; 910 910 911 911 array<VkClearValue, 2> clearValues = {}; 912 clearValues[0].color = { { 0. 45f, 0.55f, 0.60f, 1.00f } };912 clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; 913 913 clearValues[1].depthStencil = { 1.0f, 0 }; 914 914 … … 978 978 createSwapChain(); 979 979 createImageViews(); 980 createRenderPass();981 982 createCommandPools();983 980 984 981 // The depth buffer does need to be recreated with the swap chain since its dimensions depend on the window size … … 987 984 depthImage, graphicsQueue); 988 985 986 createRenderPass(); 987 createCommandPools(); 989 988 createFramebuffers(); 989 createCommandBuffers(); 990 createSyncObjects(); 990 991 991 992 // TODO: Update pipelines here 992 993 createCommandBuffers();994 995 createSyncObjects();996 993 997 994 imageIndex = 0; -
sdl-game.hpp
rcefdf23 re469aed 107 107 bool shouldRecreateSwapChain; 108 108 109 // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now 110 VkDescriptorPool imguiDescriptorPool; 111 109 112 /*** High-level vars ***/ 110 113 … … 118 121 119 122 // TODO: Make a separate singleton Timer class 120 // It could also deal with the steady_clock vs high_resolution_clock issue121 123 time_point<steady_clock> startTime; 122 124 float fpsStartTime, curTime; … … 142 144 void createSwapChain(); 143 145 void createImageViews(); 146 void createResourceCommandPool(); 147 void createImageResources(); 148 VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section 144 149 void createRenderPass(); 145 VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section146 void createResourceCommandPool();147 150 void createCommandPools(); 148 151 void createFramebuffers(); 149 152 void createCommandBuffers(); 150 153 void createSyncObjects(); 154 155 void initImGuiOverlay(); 156 void cleanupImGuiOverlay(); 151 157 152 158 void renderFrame(ImDrawData* draw_data); … … 167 173 void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height)); 168 174 void quitGame(); 169 170 // Pipeline variables. Hopefully, I can eventually use the GraphicsPipeline_Vulkan class for the imgui pipeline171 VkDescriptorPool descriptorPool;172 175 }; 173 176
Note:
See TracChangeset
for help on using the changeset viewer.