On MacOS X, you can display OpenGL for any NSView
object of your choice by simply creating an NSOpenGLContext
and then typing -setView:
on it. However, you can associate one view with only one OpenGL context at any time. My question is: if I want to display OpenGL in two different views in one window (or, possibly, in two different windows), I have two options:
Create one context and always change the view by calling setView
appropriately every time I want to display another view. This will even work if the views are in different windows or on different screens.
Create two NSOpenGLContext
and map one view to one. These two contexts can be separated, which means that most resources (e.g., textures, buffers, etc.) will be available in both views without losing twice as much memory. However, in this case, I have to constantly switch the current context every time I want to render to a different view by calling -makeCurrentContext
in the correct context before making any OpenGL calls.
In the past, I used both options, each of them worked well for my needs, however I asked myself which way is better in terms of performance, compatibility, etc. I read that context switching is actually terribly slow, or at least it was very slow in the past, it may have changed meanwhile. This may depend on how much data is associated with the context (for example, resources), since the inclusion of an active context can lead to the transfer of data between the system memory and the GPU memory.
On the other hand, switching views can be very slow, especially if it can cause the main visualizer to change; for example, if your two views are part of two different windows located on two different screens that are controlled by two different graphics cards. Even if the renderer hasnโt changed, I donโt know if the system will perform expensive OpenGL customization / cleaning when switching views, for example creating / destroying rendering / framebuffer objects.
Mecki source share