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.
OpenGL not rendering on macOS Mojave
2.8k views Asked by Elviss Strazdins At
3
There are 3 answers
0
On
On my NSView subclass which manages the NSOpenGLContext manually I needed to call NSView.displayIfNeeded instead of NSView.display for buffer swapping. Overriding NSView.setLayer and calling NSOpenGLContext.update did not help.
Please note my usage is like SDL where I use a custom run loop, so this may not have been the case in the posters program.
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'swantsLayertoNO,NSViewstill 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 frameNSViewhas 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 theNSViewis set, like this: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