GLXBadContextTag from glXMakeCurrent

Long story short: I’m writing to ask if this is a NVIDIA driver error.

Details below:

I’m debugging the following error syndrome:

Unable to create OpenGL context >= 3.0
qemu-system-x86_64: virgl could not be initialized: 22

I can see the failure in glXMakeCurrent() caused the SDL_GL_CreateContext() to give up:

823	    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
824	        X11_GL_DeleteContext(_this, context);
825	        return NULL;
826	    }

The error appears to be a GLXBadContextTag according to the following call-stack:

670	    if (X11_XGetErrorText(d, errorCode, x11_error_locale, sizeof(x11_error_locale)) == Success) {
(gdb) p errorCode 
$11 = 162
(gdb) n
671	        x11_error = SDL_iconv_string("UTF-8", "", x11_error_locale, SDL_strlen(x11_error_locale) + 1);
(gdb) n
674	    if (x11_error) {
(gdb) p x11_error
$12 = 0x55555a6c8200 "GLXBadContextTag"
(gdb) bt
#0  X11_GL_ErrorHandler (d=<optimized out>, e=<optimized out>) at ./src/video/x11/SDL_x11opengl.c:674
#1  0x00007ffff4c1741b in _XError () at /lib/x86_64-linux-gnu/libX11.so.6
#2  0x00007ffff4c13fc7 in  () at /lib/x86_64-linux-gnu/libX11.so.6
#3  0x00007ffff4c153df in _XReply () at /lib/x86_64-linux-gnu/libX11.so.6
#4  0x00007fffec9b9e70 in  () at /lib/x86_64-linux-gnu/libGLX_nvidia.so.0
#5  0x00007fffec9bbeb1 in  () at /lib/x86_64-linux-gnu/libGLX_nvidia.so.0
#6  0x00007fffeca657e3 in  () at /lib/x86_64-linux-gnu/libGLX.so.0
#7  0x00007fffeca67281 in  () at /lib/x86_64-linux-gnu/libGLX.so.0
#8  0x00007ffff4e256f3 in X11_GL_MakeCurrent (_this=_this@entry=0x555557478830, window=window@entry=0x5555587dd6b0, context=context@entry=0x555558e1f6a8)
    at ./src/video/x11/SDL_x11opengl.c:849
#9  0x00007ffff4e259eb in X11_GL_CreateContext (_this=0x555557478830, window=0x5555587dd6b0) at ./src/video/x11/SDL_x11opengl.c:823
#10 0x00007ffff4dfe370 in SDL_GL_CreateContext_REAL (window=0x5555587dd6b0) at ./src/video/SDL_video.c:4116
#11 0x00007ffff7fa0fc0 in sdl2_gl_create_context (dgc=0x5555574cf330, params=0x7fffffffd730) at ui/sdl2-gl.c:161
#12 0x00007fffeedee0b3 in virgl_create_context (opaque=<optimized out>, scanout_idx=<optimized out>, params=<optimized out>) at hw/display/virtio-gpu-virgl.c:1011

The error code is mysterious. I can see it mentioned in GLX v1.3 spec[1], related to glXMakeCurrent() and glXMakeContextCurrent(). However, in GLX v1.4 spec[2], it appears to go away together with the GLX_CONTEXT_TAG concept and suggests a faulty GLX implementation:

The following error codes may be generated by a faulty GLX implementation,
but would not normally be visible to clients:
GLXBadContextTag A rendering request contains an invalid context tag.
(Context tags are used to identify contexts in the protocol.)

Since I can see libGLX_nvidia.so in the callstack, I wonder if this could be a Nvidia driver error?
I’m currently with 535.230.02, but both 550 && 570 are not working too.

BTW: In my case, both gtk && sdl ui are not working for qemu when virgl is enabled.

[1] https://registry.khronos.org/OpenGL/specs/gl/glxencode1.3.pdf
[2] https://registry.khronos.org/OpenGL/specs/gl/glx1.4.pdf

Anyone can help me?

I checked a bit further. It turns out that the SDL2 library is able to create GL 2.1 context just fine. Once the client requested the GL4.6 context, it began to fail with GLXBadContextTag.

The qemu GTK UI does not work properly either. All it gave me was a black window.

I tried to play with a demo code testgl2 that comes with SDL2. It runs just fine out of box when it does not specify the GL context version and default to 2.1. Once I force it to request version 4.6, it no longer draw the spinning cube as it used to be.

The behavior is not identical with the qemu case though. In the SDL2 testgl2 demo, it appears to create 4.6 context properly as the app does not quit immediately on the return check from CreateContext. However, the context does not appear to be fully functional as implied by the following output:

INFO: Screen BPP    : 24
INFO: Swap Interval : 0
INFO: Window Size   : 640,480
INFO: Draw Size     : 640,480
INFO: 
INFO: Vendor        : NVIDIA Corporation
INFO: Renderer      : NVIDIA GeForce RTX 3090/PCIe/SSE2
INFO: Version       : 4.6.0 NVIDIA 535.230.02
INFO: Extensions    : (null)
INFO: 
ERROR: Failed to get SDL_GL_RED_SIZE: OpenGL error: GL_INVALID_ENUM
INFO: SDL_GL_GREEN_SIZE: requested 5, got 8
INFO: SDL_GL_BLUE_SIZE: requested 5, got 8
INFO: SDL_GL_DEPTH_SIZE: requested 16, got 24
INFO: 5543.54 frames per second

Note the Extensions line is NULL with 4.6 context. It used to be long list with 2.1 context.
The error on SDL_GL_RED_SIZE is also specific to 4.6.

I don’t know if the drawing code for GL 2.1 is expected to work with GL4.6 also. The app presents a black window while the FPS counter appears to count. What does it mean?