Implementing DearImgui to a Vulkan engine

文章推薦指數: 80 %
投票人數:10人

Those 2 are covered by the example implementations, so we are going to use those. Compiling. If you are using the tutorial code, there is already an imgui Cmake ... LinkVulkanGuideMenuHomeintroductionVulkanAPIVulkanUsageProjectlayoutandlibraries0.InitialSetupBuildingProjectCodeWalkthrough1.InitializationandRenderLoopVulkanInitializationVulkanInitializationCodeExecutingVulkanCommandsSettingupVulkancommandsRenderpassesSettinguprenderpasscodeRenderingLoopMainloopCode2.Thegraphicspipeline,HelloTriangleTherenderpipelineSettingupTriangleshadersSettinguprenderpipelinePassingdatabetweenshaderstagesTogglingshadersCleanupanddeletionqueue3.DrawingmeshesVertexbuffersImplementingvertexbuffersPushConstantsOBJLoadingSettingupdepthbufferSceneManagement4.Buffers,Shaderinput/outputDoublebufferingDescriptorSetsSettingupdescriptorsetsDynamicDescriptorsetsStoragebuffers5.TexturesMemorytransfersVulkanImagesLoadingImagesDrawingImagesGPUDrivenRenderingGPUDrivenRenderingOverviewEnginearchitectureoverviewDrawIndirectComputeShadersMaterialSystemMeshRenderingComputebasedCullingExtraChapterImplementingDearImguitoaVulkanengineImplementinganassetsystem.AthinabstractionfordescriptorsetsConfigurableoptionsthroughaCVARsystemMultithreadingforgameenginesGreatresourcesSearchVulkanSpecGithubRepoTutorialcode📖ExtraChapterImplementingDearImguitoaVulkanenginehttps://github.com/ocornut/imguiDearImguiisoneofthebestdebuguserinterfacelibrariesaround.Usingitmakesitveryeasytocreatedebugwindowswithwidgetsofvariouskinds.Thisguidewilluseafewthingsfromchapter5code,butcanbedonestandalonejustfine.Imguiitselfisaportablelibrary,butitdoesn’tdoanyuserinteractionorrenderingbyitself,youneedtohookitintoyourrendererorinputsystem.Thelibrarycomeswithasetofexampleimplementationsthathookthoseintoimgui.WeareusingVulkanforrendering,andSDLforuserinputevents.Those2arecoveredbytheexampleimplementations,sowearegoingtousethose.CompilingIfyouareusingthetutorialcode,thereisalreadyanimguiCmaketargetthatwillcompileimgui.Ifyouaren’t,youneedtoaddittoyourbuildsystem.Thefilesthatwearegoingtoneedtocompileare:target_sources(imguiPRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui.h" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui_demo.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui_draw.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui_widgets.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui_impl_vulkan.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui_impl_sdl.cpp" ) Andalsoaddingtheimguidirectorytotheincludepath.Inhere,weareaddingthecoreimguilibrarycode(imgui.cpp,demo.cpp,draw.cpp,widgets.cpp),andtheimplementationsforbothVulkanandSDL.Makesurethatyoulinktothatimguitargetfromyourcmakeorbuildsystem.Youcanalsojustaddthosefilestoyourbuildforthemainexecutable.InitializingImguiThereareafewthingsthatweneedtoinitializebeforebeingabletouseit.Forthat,createainit_imgui()function,andmakesuretocallitaspartofyourinitialization.Hastobeaftervulkanisfullyinitialized. voidVulkanEngine::init_imgui() { //1:createdescriptorpoolforIMGUI //thesizeofthepoolisveryoversize,butit'scopiedfromimguidemoitself. VkDescriptorPoolSizepool_sizes[]= { {VK_DESCRIPTOR_TYPE_SAMPLER,1000}, {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,1000}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,1000}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER,1000}, {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,1000}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,1000}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC,1000}, {VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,1000} }; VkDescriptorPoolCreateInfopool_info={}; pool_info.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; pool_info.flags=VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; pool_info.maxSets=1000; pool_info.poolSizeCount=std::size(pool_sizes); pool_info.pPoolSizes=pool_sizes; VkDescriptorPoolimguiPool; VK_CHECK(vkCreateDescriptorPool(_device,&pool_info,nullptr,&imguiPool)); //2:initializeimguilibrary //thisinitializesthecorestructuresofimgui ImGui::CreateContext(); //thisinitializesimguiforSDL ImGui_ImplSDL2_InitForVulkan(_window); //thisinitializesimguiforVulkan ImGui_ImplVulkan_InitInfoinit_info={}; init_info.Instance=_instance; init_info.PhysicalDevice=_chosenGPU; init_info.Device=_device; init_info.Queue=_graphicsQueue; init_info.DescriptorPool=imguiPool; init_info.MinImageCount=3; init_info.ImageCount=3; init_info.MSAASamples=VK_SAMPLE_COUNT_1_BIT; ImGui_ImplVulkan_Init(&init_info,_renderPass); //executeagpucommandtouploadimguifonttextures immediate_submit([&](VkCommandBuffercmd){ ImGui_ImplVulkan_CreateFontsTexture(cmd); }); //clearfonttexturesfromcpudata ImGui_ImplVulkan_DestroyFontUploadObjects(); //addthedestroytheimguicreatedstructures _mainDeletionQueue.push_function([=](){ vkDestroyDescriptorPool(_device,imguiPool,nullptr); ImGui_ImplVulkan_Shutdown(); }); } Webeginbycreatingadescriptorpoolthatimguineeds.Havingadescriptorpooljustforimguiistheeasiest.Whilethisdescriptorpoolwith1000ofeverythingislikelygoingtobeoversized,itwon’treallymatter.Then,weneedtocallthefunctionsthatinitializeimguiitself,andtheimplementationsforvulkanandforSDLOntheVulkanimplementation,thereareafewthingsthatwehavetohook.VkInstance,VkPhysicalDevice,VkDevice,theVkQueueforgraphics,andthedescriptorpoolwejustcreated.Imagecountisfortheoverlappingofthecommands.Usethesamevariablesthatyouusewhencreatingyourswapchain.Onceimguiisinitialized,thelastthingistoexecuteacommandtouploadthefonts.Inhereweareusingtheimmediatesubmitlambdathatwedidonchapter5,butyoucanreplacethatwithwhateverwayyouhaveinyourenginetoexecuteacommandinablockingway.Oncethecommandhasfullyfinishedexecution(immediatesubmitblocksonfence),youcandestroythefontuploadobjectstoclearthestagingbuffers.Thenyouaddthedeletionofthedescriptorpoolandimguivulkanitself.HookingimguiWithimguiinitialized,wenowhookitintothemainloopoftheengine.//mainloop while(!bQuit) { //Handleeventsonqueue while(SDL_PollEvent(&e)!=0) { ImGui_ImplSDL2_ProcessEvent(&e); //othereventhandling } //imguinewframe ImGui_ImplVulkan_NewFrame(); ImGui_ImplSDL2_NewFrame(_window); ImGui::NewFrame(); //imguicommands ImGui::ShowDemoWindow(); //yourdrawfunction draw(); } WebeginbycallingtheNewFramefunctionsforVulkan,SDL,andbaselibrary,afterthat,wecanstartexecutingimguithings.YoucancallimguifunctionsatanypointbetweenImgui::NewFrame(),andwhentherenderfunctioniscalled.Lastthingistorendertheimguiobjects.Onthedraw()function,wecallImGui::Render();atthestart.Aspartofyourmainrenderpass,callImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(),cmd);rightbeforeyouendit.Thiswillmakeimguirenderaspartasyourmainpass.IfyouhaveanUIpassofsomekind,that’sagoodplacetoputit.That’sreallyallthatwasneeded,soenjoyusingimgui!



請為這篇文章評分?