Creating an OpenGL 3.2+ context (Easy fix included)

If you want to use an OpenGL debug tool like https://renderdoc.org/, it’s not possible with the current JUCE version. RenderDoc simply reports this:

OpenGL Context not created via CreateContextAttribs. Capturing disabled.
Only OpenGL 3.2+ contexts are supported.

Now, the cause of this lies within juce::OpenGLContext::NativeContext. It uses the outdated wgl context creation.

 renderContext = wglCreateContext (dc);

There is a really easy fix with just a few changes. wglCreateContextAttribsARB

This method simply uses additional attributes to request a specific OpenGL context version.

HGLRC wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList)

Here are the necessary steps to implement it in juce::OpenGLContext::NativeContext.

Step 1 - Declare the extension function

JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB, HGLRC, (HDC, HGLRC, const int*))

Step 2 - Initialize the function

    void initialiseGLExtensions()
    {
...
        JUCE_INIT_WGL_FUNCTION (wglCreateContextAttribsARB);
...
    }

Step 3 - Add a function to recreate the new context with parameters

void recreateContextWithAttributes()
    {
        deleteRenderContext();

        // Specify that we want to create an OpenGL 3.3 core profile context
        int gl33_attribs[] =
        {
            WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
            WGL_CONTEXT_MINOR_VERSION_ARB, 3,
            WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
            0,
        };

        renderContext = wglCreateContextAttribsARB(dc, nullptr, gl33_attribs);
    }

Step 4 - Call the function in the constructor after extension init

 if (renderContext != nullptr)
{
    makeActive();
    initialiseGLExtensions();

    recreateContextWithAttributes(); // <-- Ad this
...

Done. After this RenderDoc works perfectly and captures all GL commands.

Additionally one could use the unused OpenGLVersion value in the constructor to specify which version should be created.

Would be nice if some cool JUCE dev could add something like this :sunglasses:

2 Likes

Thanks, I’ve pushed this change to develop here: