Implementing DearImgui to a Vulkan engine
文章推薦指數: 80 %
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!
延伸文章資訊
- 1Best 16 Dear Imgui Open Source Projects
Check out the best 16Dear Imgui free open source projects. ... Imgui Go. Go wrapper library for "...
- 2Examples of Dear ImGui for Go - Open Source Libs
It provides reference implementations on how to use and integrate Dear ImGui in Go. Screenshot. L...
- 3基于imgui的Golang跨平台快速GUI - Go开发社区 | CTOLib码库
Cross platform rapid GUI framework for golang based on Dear ImGui and the great golang binding im...
- 4Dear ImGui for Go - GitHub
Go wrapper library for "Dear ImGui" (https://github.com/ocornut/imgui) - GitHub - inkyblackness/i...
- 5imgui的golang绑定----giu的组件案例 - CSDN博客
TreeNodeFlagsCollapsingHeader|imgui. ... giu基于Dear ImGui和强大的golang绑定imgui-go的用于golang的跨平台快速GUI框架。