If you create an NSView
and a custom NSOpenGLContext
on macOS Mojave, the window is not being rendered to until it is being resized. But everything works if you use NSOpenGLView
instead.
I see lots of hacks that resize the window programmatically (http://people.bath.ac.uk/abscjkw/ComputerPrograms/C++programs/OpenGL/MojaveOpenGL.cpp) before rendering into it or call [NSOpenGLContext update]
twice (https://github.com/go-gl/glfw/pull/229/commits/9e6129a572227a13ff9acb4904443d2ae7d66e77), but they seem really hacky and unreliable.
According to Apple, OpenGL is no longer supported. However, it appears v4. 1 of OpenGL was supported on many devices as of July 28, 2020.
I disassembled Apple's frameworks and found out that they have changed how OpenGL rendering works on Mojave. It seems that even if you disable layered backing by setting NSView
's wantsLayer
to NO
, NSView
still creates and attaches a layer to your view on Mojave. Resizing the window before rendering to it works because that usually results in a call to [NSOpenGLContext update]
. Calling the update twice works, because in the first frame NSView
has no layer attached to it and the update method does nothing but on the second frame, the layer is there and [NSOpenGLContext update]
actually initializes the framebuffer.
So the solution is to call the [NSOpenGLContext update]
manually whenever the layer of the NSView
is set, like this:
@interface OpenGLView: ViewMacOS
{
NSOpenGLContext* _openGLContext;
}
@end
@implementation OpenGLView
-(void)setLayer:(CALayer*)layer
{
[super setLayer:layer];
[_openGLContext update];
}
@end
I tested it and it works both on Mojave and on older versions of macOS ([NSView setLayer:]
is not being called on macOS 10.13 and older versions). Here is the complete commit I made for the Ouzel engine: https://github.com/elnormous/ouzel/commit/7e708636189d970bad6b013ecd5375cfe693f3f3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With