ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.6k stars 10.15k forks source link

Dx12 - Reuploading textures to the GPU causes a crash #7847

Closed HiImSmiley closed 1 month ago

HiImSmiley commented 1 month ago

Version/Branch of Dear ImGui:

Master - Version 1.91.0 to 1.60 - using plain dx12 example

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx12.cpp

Compiler, OS:

Windows 11 - MSVC 2022 - v143

Full config/build information:

Dear ImGui 1.91.0 (19100)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1941
define: _MSVC_LANG=201402
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000F
 HasGamepad
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

It seems like that theres a crash happening when reuploading textures to the GPU when calling ImGui_ImplDX12_InvalidateDeviceObjects().

What I want to do:

Im really unsure if I am the issue here since the crash is happening since 1.60 ( where the example got added ).

If the intended way is different for directx12, then I am very sorry but this is how I have been doing that for my dx9 and dx11 projects ( ImGui_ImplDXXX_InvalidateDeviceObjects() + ImGui_ImplDXXX_CreateDeviceObjects() ) to reupload the texture ( also like the freetype test suggests )

How to reproduce this issue(?):

Screenshots/Video:

devenv_stei3uiQWj devenv_hcDbMW5aC8 devenv_I3V0mUSngI

Minimal, Complete and Verifiable Example code:

main.cpp

[ SwapchainOccluded checks ]

 static bool should_reupload_textures = false;

 if (should_reupload_textures)
 {
     should_reupload_textures = false;

     [ adding new fonts + building them ] - not needed to cause the crash

     ImGui_ImplDX12_InvalidateDeviceObjects();
     ImGui_ImplDX12_CreateDeviceObjects();
 }

 // Start the Dear ImGui frame
 ImGui_ImplDX12_NewFrame();
 ImGui_ImplWin32_NewFrame();
 ImGui::NewFrame();

[ Demo and simple window setup ]

if (ImGui::Button("Button"))
{
    counter++;
    should_reupload_textures = true;
}
GamingMinds-DanielC commented 1 month ago

In the DX12 example there is a function WaitForLastSubmittedFrame() that is called before shutting down or resizing. In DX12 you need to synchronize resource access yourself, so waiting for resources to no longer be in use before manipulating or destroying them is required. If you call WaitForLastSubmittedFrame() before ImGui_ImplDX12_InvalidateDeviceObjects() you should be good.

HiImSmiley commented 1 month ago

That works! Thank you very much!