Skip to content

Commit

Permalink
GLSupport: Win32 - use CreateContextAttribs
Browse files Browse the repository at this point in the history
for context ceation. Similar to GLX. Allows using renderdoc.
  • Loading branch information
paroj committed Nov 29, 2020
1 parent 27b8feb commit 7d0c8e7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 37 deletions.
4 changes: 3 additions & 1 deletion RenderSystems/GLSupport/include/win32/OgreWin32Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace Ogre {
{
public:
Win32Context(HDC HDC,
HGLRC Glrc);
HGLRC Glrc,
Win32GLSupport &glsupport);
virtual ~Win32Context();

/** See GLContext */
Expand All @@ -52,6 +53,7 @@ namespace Ogre {
protected:
HDC mHDC;
HGLRC mGlrc;
Win32GLSupport &mGLSupport;
};
}

Expand Down
2 changes: 2 additions & 0 deletions RenderSystems/GLSupport/include/win32/OgreWin32GLSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace Ogre

bool selectPixelFormat(HDC hdc, int colourDepth, int multisample, bool hwGamma);

HGLRC createNewContext(HDC hdc, HGLRC shareList);

virtual unsigned int getDisplayMonitorCount() const;
private:
Win32Window *mInitialWindow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Ogre {
class _OgreGLExport Win32PBuffer : public GLPBuffer
{
public:
Win32PBuffer(PixelComponentType format, size_t width, size_t height);
Win32PBuffer(PixelComponentType format, size_t width, size_t height, Win32GLSupport &glsupport);
~Win32PBuffer();

GLContext *getContext() const { return mContext; }
Expand Down
28 changes: 8 additions & 20 deletions RenderSystems/GLSupport/src/win32/OgreWin32Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ THE SOFTWARE.
#include "OgreException.h"
#include "OgreGLRenderSystemCommon.h"
#include "OgreRoot.h"
#include "OgreWin32GLSupport.h"

namespace Ogre {

Win32Context::Win32Context(HDC HDC, HGLRC Glrc):
Win32Context::Win32Context(HDC HDC, HGLRC Glrc, Win32GLSupport &glsupport):
mHDC(HDC),
mGlrc(Glrc)
mGlrc(Glrc),
mGLSupport(glsupport)
{
}

Expand All @@ -61,30 +63,16 @@ namespace Ogre {

GLContext* Win32Context::clone() const
{
HGLRC oldrc = wglGetCurrentContext();
// Create new context based on own HDC
HGLRC newCtx = wglCreateContext(mHDC);
HGLRC newCtx = mGLSupport.createNewContext(mHDC, oldrc);

if (!newCtx)
{
OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
"Error calling wglCreateContext", "Win32Context::clone");
}

HGLRC oldrc = wglGetCurrentContext();
HDC oldhdc = wglGetCurrentDC();
wglMakeCurrent(NULL, NULL);
// Share lists with old context
if (!wglShareLists(mGlrc, newCtx))
{
String errorMsg = translateWGLError();
wglDeleteContext(newCtx);
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("wglShareLists() failed: ") + errorMsg, "Win32Context::clone");
}

// restore old context
wglMakeCurrent(oldhdc, oldrc);


return new Win32Context(mHDC, newCtx);
return new Win32Context(mHDC, newCtx, mGLSupport);
}

void Win32Context::releaseContext()
Expand Down
56 changes: 55 additions & 1 deletion RenderSystems/GLSupport/src/win32/OgreWin32GLSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

#include <GL/wglext.h>

static PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB = 0;

namespace Ogre {
GLNativeSupport* getGLSupport(int profile)
{
Expand Down Expand Up @@ -310,7 +312,10 @@ namespace Ogre {
HDC oldhdc = wglGetCurrentDC();
// if wglMakeCurrent fails, wglGetProcAddress will return null
wglMakeCurrent(hdc, hrc);


_wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)
wglGetProcAddress("wglCreateContextAttribsARB");

PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
(PFNWGLGETEXTENSIONSSTRINGARBPROC)
wglGetProcAddress("wglGetExtensionsStringARB");
Expand Down Expand Up @@ -459,6 +464,55 @@ namespace Ogre {
return (format && SetPixelFormat(hdc, format, &pfd));
}

HGLRC Win32GLSupport::createNewContext(HDC hdc, HGLRC shareList)
{
HGLRC glrc = NULL;

int profile;
int minVersion;
int maxVersion = 5;

switch(mContextProfile) {
case CONTEXT_COMPATIBILITY:
profile = WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
minVersion = 1;
maxVersion = 3; // requesting 3.1 might return 3.2 core profile
break;
case CONTEXT_ES:
profile = WGL_CONTEXT_ES2_PROFILE_BIT_EXT;
minVersion = 2;
break;
default:
profile = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
minVersion = 3;
break;
}

int context_attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, maxVersion,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
WGL_CONTEXT_PROFILE_MASK_ARB, profile,
0
};

glrc = _wglCreateContextAttribsARB ? _wglCreateContextAttribsARB(hdc, shareList, context_attribs) : wglCreateContext(hdc);

if (!glrc)
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglCreateContext failed: " + translateWGLError());

if(!_wglCreateContextAttribsARB && shareList && shareList != glrc)
{
// Share lists with old context
if (!wglShareLists(shareList, glrc))
{
wglDeleteContext(glrc);
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglShareLists() failed: "+ translateWGLError());
}
}

return glrc;
}

unsigned int Win32GLSupport::getDisplayMonitorCount() const
{
if (mMonitorInfoList.empty())
Expand Down
4 changes: 2 additions & 2 deletions RenderSystems/GLSupport/src/win32/OgreWin32RenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ THE SOFTWARE.

namespace Ogre {

Win32PBuffer::Win32PBuffer(PixelComponentType format, size_t width, size_t height):
Win32PBuffer::Win32PBuffer(PixelComponentType format, size_t width, size_t height, Win32GLSupport &glsupport):
GLPBuffer(format, width, height),
mContext(0)
{
createPBuffer();

// Create context
mContext = new Win32Context(mHDC, mGlrc);
mContext = new Win32Context(mHDC, mGlrc, glsupport);
#if 0
if(mUseBind)
{
Expand Down
15 changes: 3 additions & 12 deletions RenderSystems/GLSupport/src/win32/OgreWin32Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,8 @@ namespace Ogre {
}
if (mOwnsGLContext)
{
mGlrc = wglCreateContext(mHDC);
if (!mGlrc)
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
"wglCreateContext failed: " + translateWGLError(), "Win32Window::create");
}

if (old_context && old_context != mGlrc)
{
// Share lists with old context
if (!wglShareLists(old_context, mGlrc))
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglShareLists() failed", " Win32Window::create");
// New context is shared with previous one
mGlrc = mGLSupport.createNewContext(mHDC, old_context);
}

if (!wglMakeCurrent(mHDC, mGlrc))
Expand All @@ -505,7 +496,7 @@ namespace Ogre {
}

// Create RenderSystem context
mContext = new Win32Context(mHDC, mGlrc);
mContext = new Win32Context(mHDC, mGlrc, mGLSupport);

mActive = true;
setHidden(hidden);
Expand Down

0 comments on commit 7d0c8e7

Please sign in to comment.