bkaradzic / bgfx

Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
https://bkaradzic.github.io/bgfx/overview.html
BSD 2-Clause "Simplified" License
14.61k stars 1.92k forks source link

A few shutdown & re-init bugs #3320

Open stakira opened 4 days ago

stakira commented 4 days ago

A few issues encountered when trying to get shutdown and re-init to work.

The program simply inits bgfx, shuts it down, then inits it again using the same Init struct.

  1. Single-thread mode, 2nd init() ends up entering multi-thread mode and freezes. There are two conflicting log lines at the end of below:
    I0705 02:31:26.442129   71144 app.cc:309] Paused: true, Suspended: true
    I0705 02:31:26.445782   71144 bgfx.cpp:3611] BGFX Shutdown...
    I0705 02:31:26.525306   71144 bgfx.cpp:3620] BGFX Shutdown complete.
    I0705 02:31:30.762280   71144 app.cc:309] Paused: false, Suspended: false
    I0705 02:31:30.762474   71144 bgfx.cpp:3559] BGFX Init...
    I0705 02:31:30.762566   71144 bgfx.cpp:3566] BGFX Version 1.128.8775 (commit: a476c5b9a42d3779af59a0099d4d222fa8898d36)
    I0705 02:31:30.772927   71144 bgfx.cpp:1736] BGFX Reset back-buffer swap chain:
    I0705 02:31:30.773095   71144 bgfx.cpp:1745] BGFX       1280x720, format: RGBA8, numBackBuffers: 2, maxFrameLatency: 0
    I0705 02:31:30.773172   71144 bgfx.cpp:1747] BGFX       [ ] MSAAx1
    I0705 02:31:30.773249   71144 bgfx.cpp:1748] BGFX       [ ] Fullscreen
    I0705 02:31:30.773321   71144 bgfx.cpp:1749] BGFX       [x] V-sync
    I0705 02:31:30.773387   71144 bgfx.cpp:1750] BGFX       [ ] Max Anisotropy
    I0705 02:31:30.773444   71144 bgfx.cpp:1751] BGFX       [ ] Capture
    I0705 02:31:30.773504   71144 bgfx.cpp:1752] BGFX       [ ] Flush After Render
    I0705 02:31:30.773550   71144 bgfx.cpp:1753] BGFX       [ ] Flip After Render
    I0705 02:31:30.773595   71144 bgfx.cpp:1754] BGFX       [ ] sRGB Back Buffer
    I0705 02:31:30.773639   71144 bgfx.cpp:1755] BGFX       [ ] Transparent Back Buffer
    I0705 02:31:30.773684   71144 bgfx.cpp:1756] BGFX       [ ] HDR10
    I0705 02:31:30.773733   71144 bgfx.cpp:1757] BGFX       [ ] Hi-DPI
    I0705 02:31:30.773788   71144 bgfx.cpp:1758] BGFX       [ ] Depth Clamp
    I0705 02:31:30.773832   71144 bgfx.cpp:1759] BGFX       [ ] Suspend
    I0705 02:31:30.776838   71144 bgfx.cpp:1932] BGFX Application called bgfx::renderFrame directly, not creating render thread.
    I0705 02:31:30.776922   71144 bgfx.cpp:1948] BGFX Running in multi-threaded mode

Looks like it's relevant to the BGFX_API_THREAD_MAGIC check

  1. Multi-thread mode. Mostly works but after 2nd init clearing stops working, with all backends. (fixed, user error)

  2. In both modes, below warning appears during shutdown:

d3d11 backend:

BGFX WARN 0000021218776C90 RefCount is 3 (expected 0).

d3d12 backend:

BGFX WARN 000001D96E7DB628 RefCount is 530 (expected 0).

GL and Vulkan seems fine. Metal and GLES not tested.

This issue is not as important as the first 2 since shutdown & re-init is not as useful on Windows.

bkaradzic commented 4 days ago

Check your setup, it looks like something is attaching to your D3D device.

stakira commented 4 days ago

What do you mean by attaching to? Which issue are you responding to? If you mean issue 3, I only mean to report the potential issue and I can live with it. Issue 1 and issue 2 are more blocking.

Reproduced issue 1 on Mac + Metal. Issue 2... never got multi-threading to work with Mac + Metal.

Edit: issue 2 is my mistake not calling reset and set view rect after re-init.

Still not understanding why 1 happens, but I think at minimum the below log message is incorrect. It is not guaranteed to be single thread mode when the message says so. The Helloworld demo appears to be so on Mac.

            BX_TRACE("Application called bgfx::renderFrame directly, not creating render thread.");
            m_singleThreaded = true
                && ~BGFX_API_THREAD_MAGIC == s_threadIndex
                ;