Skip to content

Commit

Permalink
[GL3+] add support for currentGLContext for macOS
Browse files Browse the repository at this point in the history
- Support the parameter 'currentGLContext' on macOS for the GL3+ RS
- Initialise pointers to nil in CocoaWindow::create
- Add checks to ensure that attempts to create new Cocoa windows or set a view on an OpenGL context only occur on the main thread

Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
  • Loading branch information
srmainwaring authored and darksylinc committed Aug 22, 2021
1 parent 9b99e0d commit 79be9e9
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions RenderSystems/GL3Plus/src/windowing/OSX/OgreOSXCocoaWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ - (BOOL)acceptsFirstResponder
NSString *windowTitle = [NSString stringWithCString:name.c_str() encoding:NSUTF8StringEncoding];
int winxPt = 0, winyPt = 0;
int colourDepth = 32;
NSOpenGLContext *externalGLContext;
NSObject* externalWindowHandle; // NSOpenGLView, NSView or NSWindow
NSOpenGLContext *externalGLContext = nil;
NSObject* externalWindowHandle = nil; // NSOpenGLView, NSView or NSWindow
NameValuePairList::const_iterator opt;

bool useCurrentGLContext = false;

mIsFullScreen = fullScreen;

if(miscParams)
Expand Down Expand Up @@ -181,6 +182,10 @@ - (BOOL)acceptsFirstResponder
if(opt != miscParams->end())
mContentScalingFactor = StringConverter::parseReal(opt->second);

opt = miscParams->find("currentGLContext");
if(opt != miscParams->end())
useCurrentGLContext = StringConverter::parseBool(opt->second);

opt = miscParams->find("externalGLContext");
if(opt != miscParams->end())
externalGLContext = (__bridge NSOpenGLContext *)(void*)StringConverter::parseSizeT(opt->second);
Expand Down Expand Up @@ -213,6 +218,12 @@ - (BOOL)acceptsFirstResponder
mGLContext = externalGLContext;
mGLPixelFormat = externalGLContext.pixelFormat;
}
else if(useCurrentGLContext)
{
CGLContextObj currentGLContext = CGLGetCurrentContext();
mGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:currentGLContext];
mGLPixelFormat = mGLContext.pixelFormat;
}
else
{
NSOpenGLPixelFormatAttribute attribs[30];
Expand Down Expand Up @@ -339,8 +350,21 @@ - (BOOL)acceptsFirstResponder
[(id)mView setOpenGLContext:mGLContext];

// Repeat what -[NSOpenGLView setOpenGLContext:] does in case mView is not NSOpenGLView
// Must be called on the main (UI) thread or else a hard crash occurs
// on macOS Catalina and later.
if([mGLContext view] != mView)
[mGLContext setView:mView];
{
if ([NSThread isMainThread])
{
[mGLContext setView:mView];
}
else
{
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
"NSOpenGLContext setView must be called on main (UI) thread.",
"CocoaWindow::create");
}
}
[mGLContext makeCurrentContext];

#if OGRE_DEBUG_MODE
Expand Down Expand Up @@ -602,7 +626,18 @@ - (BOOL)acceptsFirstResponder
[mGLContext makeCurrentContext];

if([mGLContext view] != mView)
[mGLContext setView:mView];
{
if ([NSThread isMainThread])
{
[mGLContext setView:mView];
}
else
{
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
"NSOpenGLContext setView must be called on main (UI) thread.",
"CocoaWindow::swapBuffers");
}
}

[mGLContext flushBuffer];
CGLUnlockContext((CGLContextObj)[mGLContext CGLContextObj]);
Expand Down Expand Up @@ -653,10 +688,23 @@ - (BOOL)acceptsFirstResponder
else
windowRect = NSMakeRect(0.0, 0.0, widthPt, heightPt);

mWindow = [[OgreGL3PlusWindow alloc] initWithContentRect:windowRect
styleMask:mIsFullScreen ? NSBorderlessWindowMask : NSResizableWindowMask|NSTitledWindowMask
@try
{
mWindow = [[OgreGL3PlusWindow alloc] initWithContentRect:windowRect
styleMask:mIsFullScreen ?
NSWindowStyleMaskBorderless :
NSWindowStyleMaskResizable|NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:YES];
defer:YES];
}
@catch (NSException *exception)
{
std::string msg([exception.reason UTF8String]);
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
"Failed to create window : " + msg,
"CocoaWindow::createNewWindow");
}

[mWindow setTitle:[NSString stringWithCString:title.c_str() encoding:NSUTF8StringEncoding]];
mWindowTitle = title;

Expand Down

0 comments on commit 79be9e9

Please sign in to comment.