LukasBanana / LLGL

Low Level Graphics Library (LLGL) is a thin abstraction layer for the modern graphics APIs OpenGL, Direct3D, Vulkan, and Metal
BSD 3-Clause "New" or "Revised" License
2.03k stars 135 forks source link

[DX11] Crash on resize with multiple `CommandBuffer`'s #109

Closed duckdoom4 closed 5 months ago

duckdoom4 commented 5 months ago

I ran into a crash when resizing the window. After some debugging I figured out it was because I was using multiple CommandBuffers.

I took the code from the ImGui Example. And added it to the PBR example.

The resulting code would look like this:

immediateCommandBuffer_ = renderer_->CreateCommandBuffer(LLGL::CommandBufferFlags::ImmediateSubmit);
commandBuffer_ = renderer_->CreateCommandBuffer();
commandBuffer_->Begin();
// Draw scene ...
commandBuffer_->End();
commandQueue_->Submit(*commandBuffer);

immediateCommandBuffer_->Begin();
{
    immediateCommandBuffer_->BeginRenderPass(*swapChain_);
    {
        RenderImGui();
    }
    immediateCommandBuffer_->EndRenderPass();
}
immediateCommandBuffer_->End();

If I then attempt to resize the window, D3D11SwapChain::ResizeBackBuffer @ swapChain_->ResizeBuffers will throw with 'failed to resize DXGI swap-chain buffers'.

That is due to the command buffer swap. If I add a 'hacky fix' at the end of the above code, it works fine:

//... Previous code
immediateCommandBuffer_->End();

commandBuffer_->Begin();
commandBuffer_->BeginRenderPass(*_swapChain);
commandBuffer_->EndRenderPass();
commandBuffer_->End();

This forces the state to reset to the deferred command list, which now makes it possible to release the internal references to the backbuffer.

LukasBanana commented 5 months ago

Thanks for bringing it up and showing an example. I was able to reproduce the issue even without integrating ImGui and submitted a fix with 01b5d07.