LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.67k stars 631 forks source link

BGFX Vsync issue with Vulkan #803

Closed kappaOne closed 1 year ago

kappaOne commented 1 year ago

Version

3.3.2 (nightly)

Platform

Linux x64

JDK

Zulu OpenJDK 17

Module

BGFX, Vulkan, GLFW

Bug description

I am having issues using VSync with BGFX when the Vulkan backend is enabled.

When using BGFX, you can enable VSync using BGFX_RESET_VSYNC as a reset flag. Initially it appears to start and run fine, however when doing any window (GLFW specific) events, such as resizing or moving the window, the application begins to freeze and then eventually lead to a hang and/or crash.

Steps to reproduce: 1) Run the ExampleBGFX (NanoVG) demo. 2) Ensure the BGFX.BGFX_RESET_VSYNC is set as a reset flag, so that Vsync is enabled. 3) Ensure the Vulkan renderer is being used or select it manually using BGFX_RENDERER_TYPE_VULKAN with BGFXInit.type(), on linux it mostly appears to default to Vulkan. 4) Resize the window for a bit or drag it around and it will eventually cause the program to hang and/or crash.

I have reproduced the issue on multiple computers using Linux x64, however have not yet been able to test it on Windows or any AMD graphics cards (so if anyone could test this there that would also help narrow the issue down to whether its platform or graphics card specific).

The issue does not happen when using the OpenGL renderer, BGFX_RENDERER_TYPE_OPENGL, so appears to be Vulkan renderer specific. This may be an upstream issue however, best to first try narrow it down further.

Stacktrace or crash log output

Current thread (0x00007f5440a546e0):  JavaThread "Thread-0" daemon [_thread_in_native, id=10948, stack(0x00007f5472252000,0x00007f5472a51000)]

Stack: [0x00007f5472252000,0x00007f5472a51000],  sp=0x00007f5472a4f4e8,  free space=8181k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libnvidia-glcore.so.515.65.01+0x128d9d4]

siginfo: si_signo: 11 (SIGSEGV), si_code: 1 (SEGV_MAPERR), si_addr: 0x00000000000000a8

Register to memory mapping:

RAX=0x0 is NULL
RBX=0x00007f5472a4f600 is pointing into the stack for thread: 0x00007f5440a546e0
RCX=0x0000000000000023 is an unknown value
RDX=0x000000df961d0000 is an unknown value
RSP=0x00007f5472a4f4e8 is pointing into the stack for thread: 0x00007f5440a546e0
RBP=0x0000000000000001 is an unknown value
RSI=0x00007f5472a4f510 is pointing into the stack for thread: 0x00007f5440a546e0
RDI=0x00007f544035abf0 points into unknown readable memory: 0x00007f544035a5d0 | d0 a5 35 40 54 7f 00 00
R8 =0x0 is NULL
R9 =0x0 is NULL
R10=0x00007f5433a52380: <offset 0x0000000001252380> in /usr/lib/libnvidia-glcore.so.515.65.01 at 0x00007f5432800000
R11=0x00007f5440a45740 points into unknown readable memory: 0x0000000001cdc0de | de c0 cd 01 00 00 00 00
R12=0x00007f5440a8d640 points into unknown readable memory: 0x00007f5434c8dca0 | a0 dc c8 34 54 7f 00 00
R13=0x00007f5440a26d00 points into unknown readable memory: 0x00007f5434c8dca0 | a0 dc c8 34 54 7f 00 00
R14=0x0000000000000001 is an unknown value
R15=0x00007f544035bbd0 points into unknown readable memory: 0x00007f5434c8dae8 | e8 da c8 34 54 7f 00 00
Spasi commented 1 year ago

Hey @kappaOne,

Tested on Windows 11, Zulu JDK 18, latest Nvidia drivers:

kappaOne commented 1 year ago

Hi Spasi,

Thanks for the quick testing.

After some more testing, the hanging and slow down (and eventual crash) appears to be due to sudden 100% CPU usage on the main thread.

It appears that while resizing the GLFW window using the OpenGL Renderer, VSync continues to work (consistent fps) thus throttling the main thread (application loop) as normal.

However when using the Vulkan Renderer and resizing the window, the fps becomes uncapped causing main thread (application loop) to run at 100% causing the hanging and eventually crashing the driver/app. If you stop resizing the window before it hangs the vsync capped fps returns back to normal.

Adding a simple Thread.sleep() into the application loop prevents the hanging/crashing with the Vulkan renderer. I am leaning towards this being a driver bug or a problem with the way BGFX Vulkan renderer handles resizing here (such as causing vsync to disable during resizing).

While running without vsync the Vulkan Renderer can run at full fps without causing the 100% cpu usage (even during resizing), so something with the vsync flag is triggering this issue.

kappaOne commented 1 year ago

I've now tested on an intel card and can confirm that the issues doesn't happen there, therefore this is most likely a nvidia driver bug.