Instead, switch OpenGL contexts or context context visualization context, which is preferable?

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.

+6
source share
1 answer

I explored switching context between 3 windows on Lion, where I tried to solve some performance problems with the incorrect VTK library, which in itself is already very slow.

Include rendering contexts or windows don't really matter, because there is always the overhead of both being current for the calling stream as a triple. I measured approximately 50 ms on the switch, where some operating expenses on OS / Window are also charged. This overhead also depends heavily on the location of other GL calls, because the driver may be forced to wait for commands to complete, which can be achieved manually by the blocking call to glFinish ().

The most effective setting I worked with is similar to your second, but it has two highlighted visualization streams that have a visualization context (general) and a window that is permanently attached. Context context variables / bindings are executed only once during initialization.

Streams can be monitored using some threads, such as a common barrier, which allows both threads to visualize single frames in synchronization (both are locked onto the barrier before they can be started again). Data processing should also be blocked, which can be done in one thread while stopping other rendering threads.

+4
source

Source: https://habr.com/ru/post/908454/


All Articles