Changeset 3b7d497 in opengl-game for vulkan-game.cpp
- Timestamp:
- Jan 2, 2021, 4:19:31 PM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 6493e43
- Parents:
- c324d6a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rc324d6a r3b7d497 1 1 #include "vulkan-game.hpp" 2 3 #include "IMGUI/imgui_impl_sdl.h" 4 #include "IMGUI/imgui_impl_vulkan.h" 2 5 3 6 #include <array> … … 25 28 * - IMGUI creates one command pool per framebuffer 26 29 */ 30 31 /* NOTES WHEN ADDING IMGUI 32 * 33 * Possibly cleanup the imgui pipeline in cleanupSwapchain or call some imgui function that does this for me 34 * call ImGui_ImplVulkan_RenderDrawData, without passing in a pipeline, to do the rendering 35 */ 36 37 // Put in here to use for IMGUI, but I might use something similar in other places as well 38 static void check_vk_result(VkResult result) { 39 if (result == VK_SUCCESS) 40 return; 41 fprintf(stderr, "[vulkan] Error: VkResult = %d\n", result); 42 if (result < 0) 43 abort(); 44 } 27 45 28 46 VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) { … … 189 207 createImageResources(); 190 208 createFramebuffers(); 209 210 // TODO: I think I can start setting up IMGUI here 211 // ImGui_ImplVulkan_Init will create the Vulkan pipeline for ImGui for me 212 // imgui_impl_vulkan keeps track of the imgui pipeline internally 213 // TODO: Check how the example recreates the pipeline and what code I need 214 // to copy over to do that 215 216 createImguiDescriptorPool(); 217 218 IMGUI_CHECKVERSION(); 219 ImGui::CreateContext(); 220 ImGuiIO& io = ImGui::GetIO(); (void)io; 221 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 222 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 223 224 // Setup Dear ImGui style 225 ImGui::StyleColorsDark(); 226 //ImGui::StyleColorsClassic(); 227 228 // TODO: Make call this once and save the results since it's also called when creating the logical device 229 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 230 231 ImGui_ImplSDL2_InitForVulkan(window); 232 ImGui_ImplVulkan_InitInfo init_info = {}; 233 init_info.Instance = this->instance; 234 init_info.PhysicalDevice = this->physicalDevice; 235 init_info.Device = this->device; 236 init_info.QueueFamily = indices.graphicsFamily.value(); 237 init_info.Queue = this->graphicsQueue; 238 init_info.PipelineCache = VK_NULL_HANDLE; 239 init_info.DescriptorPool = this->imguiDescriptorPool; // TODO: Create a descriptor pool for IMGUI 240 init_info.Allocator = nullptr; 241 init_info.MinImageCount = this->swapChainImageCount; 242 init_info.ImageCount = this->swapChainImages.size(); 243 init_info.CheckVkResultFn = check_vk_result; 244 ImGui_ImplVulkan_Init(&init_info, this->renderPass); 245 246 cout << "Got here" << endl; 247 248 // TODO: I think I have code in VkUtil for creating VkImages, which uses command buffers 249 // Maybe check how that code works 250 251 // Upload Fonts 252 { 253 VkResult err; 254 255 // Use any command queue 256 VkCommandPool command_pool = this->commandPool; // TODO: No need to create a separate variable. Just use this->commandPool directly 257 VkCommandBuffer command_buffer; 258 259 // Create the command buffer to load 260 VkCommandBufferAllocateInfo info = {}; 261 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; 262 info.commandPool = command_pool; 263 info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 264 info.commandBufferCount = 1; 265 err = vkAllocateCommandBuffers(this->device, &info, &command_buffer); 266 check_vk_result(err); 267 268 //err = vkResetCommandPool(this->device, command_pool, 0); // Probably not really needed here since the command pool is never used before this 269 //check_vk_result(err); 270 VkCommandBufferBeginInfo begin_info = {}; 271 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 272 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 273 err = vkBeginCommandBuffer(command_buffer, &begin_info); 274 check_vk_result(err); 275 276 ImGui_ImplVulkan_CreateFontsTexture(command_buffer); 277 278 VkSubmitInfo end_info = {}; 279 end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 280 end_info.commandBufferCount = 1; 281 end_info.pCommandBuffers = &command_buffer; 282 err = vkEndCommandBuffer(command_buffer); 283 check_vk_result(err); 284 err = vkQueueSubmit(this->graphicsQueue, 1, &end_info, VK_NULL_HANDLE); 285 check_vk_result(err); 286 287 err = vkDeviceWaitIdle(this->device); 288 check_vk_result(err); 289 ImGui_ImplVulkan_DestroyFontUploadObjects(); 290 291 // This should make the command pool reusable for later 292 err = vkResetCommandPool(this->device, command_pool, 0); 293 check_vk_result(err); 294 } 295 296 cout << "And now here" << endl; 191 297 192 298 initMatrices(); … … 584 690 585 691 createSyncObjects(); 692 693 cout << "Finished init function" << endl; 586 694 } 587 695 … … 1086 1194 1087 1195 void VulkanGame::cleanup() { 1196 ImGui_ImplVulkan_Shutdown(); 1197 ImGui_ImplSDL2_Shutdown(); 1198 ImGui::DestroyContext(); 1199 1200 destroyImguiDescriptorPool(); 1201 1088 1202 cleanupSwapChain(); 1089 1203 … … 1592 1706 currentScreen->createRenderCommands(commandBuffers[i], i); 1593 1707 1708 /**********************************************************/ 1709 1710 ImGui_ImplVulkan_NewFrame(); 1711 ImGui_ImplSDL2_NewFrame(this->window); 1712 ImGui::NewFrame(); 1713 1714 { 1715 ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once); 1716 ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once); 1717 ImGui::Begin("WndMenubar", NULL, 1718 ImGuiWindowFlags_NoTitleBar | 1719 ImGuiWindowFlags_NoResize | 1720 ImGuiWindowFlags_NoMove); 1721 ImGui::InvisibleButton("", ImVec2(155, 18)); 1722 ImGui::SameLine(); 1723 if (ImGui::Button("Main Menu")) { 1724 cout << "Clicked on the main button" << endl; 1725 //events.push(Event::GO_TO_MAIN_MENU); 1726 } 1727 ImGui::End(); 1728 } 1729 1730 ImGui::Render(); 1731 ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), commandBuffers[i]); 1732 1733 /**********************************************************/ 1734 1594 1735 vkCmdEndRenderPass(commandBuffers[i]); 1595 1736 … … 1598 1739 } 1599 1740 } 1741 } 1742 1743 void VulkanGame::createImguiDescriptorPool() { 1744 vector<VkDescriptorPoolSize> pool_sizes{ 1745 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, 1746 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, 1747 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, 1748 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, 1749 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, 1750 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, 1751 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, 1752 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, 1753 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, 1754 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, 1755 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } 1756 }; 1757 1758 VkDescriptorPoolCreateInfo pool_info = {}; 1759 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 1760 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 1761 pool_info.maxSets = 1000 * pool_sizes.size(); 1762 pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size()); 1763 pool_info.pPoolSizes = pool_sizes.data(); 1764 if (vkCreateDescriptorPool(device, &pool_info, nullptr, &imguiDescriptorPool) != VK_SUCCESS) { 1765 throw runtime_error("failed to create IMGUI descriptor pool!"); 1766 } 1767 } 1768 1769 void VulkanGame::destroyImguiDescriptorPool() { 1770 vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr); 1600 1771 } 1601 1772
Note:
See TracChangeset
for help on using the changeset viewer.