Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glFlush() vs [[self openGLContext] flushBuffer] vs glFinish vs glSwapAPPLE vs aglSwapBuffers

There are several similar OpenGL operations when working with an NSOpenGLView:

  • glFlush()
  • [[self openGLContext] flushBuffer]
  • glFinish()
  • glSwapAPPLE
  • aglSwapBuffers

When should each of these be used?

In a sample application, Apple uses glFlush(), followed by [[self openGLContext] flushBuffer]. Why do they use both of these?

What would be the right approach, if I am using a double-buffered Cocoa NSOpenGLView?

like image 981
Peter Lapisu Avatar asked Aug 11 '11 14:08

Peter Lapisu


3 Answers

Be careful! [[self openGLContext] flushBuffer] is not just an Objective-C wrapper for gFlush(). This function (- (void)flushBuffer in the Apple Documentation) works only if you set double buffer in your pixel format like

NSOpenGLPixelFormatAttribute attributes [] =
    {
        NSOpenGLPFADoubleBuffer,
        // other presets like
        // NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        // NSOpenGLPFADepthSize, 32,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] 
                                            initWithAttributes:attributes];

Otherwise you have to use glFlush(); It took me a long time until I saw the essential line in the NSOpenGLContext Class Reference.

like image 193
Michael Dorner Avatar answered Oct 24 '22 17:10

Michael Dorner


Have you looked at this? It explains when to use glFlush() and glFinish(). Both are OpenGL functions that control execution and synchronizing of commands. Generally you would want to use these functions when doing multi-threaded rendering otherwise there shouldn't be any need.

glSwapAPPLE() and aglSwapBuffers() are extensions provided by Apple to display the contents of the backbuffer to screen (on Windows it's wglSwapBuffers()). You should use either one but not both as they really do the same thing. I would stick to the AGL method as it's analogous to WGL, EGL, etc..

[[self openGLContext] flushBuffer] is probably an objective C wrapper to glFlush() from the looks of it. I can't imagine it doing anything else.

like image 2
Jasoneer Avatar answered Oct 24 '22 18:10

Jasoneer


The correct approach is just calling [[self openGLContext] flushBuffer].

like image 2
Frederik Slijkerman Avatar answered Oct 24 '22 17:10

Frederik Slijkerman