To glFlush() or not to glFlush()...?

Started by
10 comments, last by Brother Bob 19 years, 9 months ago
Ive been reading through alot of tuts and examples and alot of peoples source code along with the OpenGL Blue Book. The use of glFlush() seems to be pretty hit and miss. The GL Blue Book describes its function as forcing the execution of all presently buffered commands then clears the buffer. So my question is this: How does the use of glFlush() differ from just allowing the program to execute all the buffered commands manualy? Is there a noticable speed increase? What situtations would you use or not use glFlush() ?
C_C(Enter witty/insightful/profound remark here...)
Advertisement
To elaborate further...

It sounds to me that if you have a command that forces the execution of the buffered commands then then that command overrides some kind of wait period. Is that the case? Is there some form of default wait time before commands are executed?
C_C(Enter witty/insightful/profound remark here...)
As I understand it glFlush( ) basically waits until everything has been drawn etc.

Quote:How does the use of glFlush() differ from just allowing the program to execute all the buffered commands manualy? Is there a noticable speed increase?


Well it does pause program execution until everything has been done, which means the driver has more cpu time to do whatever it needs to do, so you may think you could get a slight speed increase. However most of the stuff will be done on the GPU, in which case glFlush will just mean your CPU is idle waiting for the GPU, which is not a good thing. What you want to do is have the GPU drawing stuff while the CPU gets on with something else such as physics or AI. The way to do this (I'm not actually entirely sure if this works, so someone please correct me if I'm wrong here) is to do all your GL stuff at the beginning of your game loop, then anything else such as physics/ai/etc and then swap the buffers, which I think implicitly calls glFush (or at least does the same thing), because it has to have everything drawn before it can do the buffer swap.
As far as I understand, the glFlush command has to do with the Client/Server configuration of OpenGL.

Basically commands get queued, if the "network" is too slow.

I add glFlush at the end of my Render proc just to sure.( Although it works Just as well without it.

Exert from the redbook:
"glFlush() ensures that the drawing commands are actually executed, rather than stored in a buffer awaiting additional OpenGL commands."

Disclaimer: I might be speaking absolute cr@p, so feel free to ignore this ;)

As per OpenGL.com:

void glFlush( void )

DESCRIPTION
Different GL implementations buffer commands in several different locations, including network buffers and the graphics accelerator itself. glFlush empties all of these buffers, causing all issued commands to be executed as quickly as they are accepted by the actual rendering engine. Though this execution may not be completed in any particular time period, it does complete in finite time.

Because any GL program might be executed over a network, or on an accelerator that buffers commands, all programs should call glFlush whenever they count on having all of their previously issued commands completed. For example, call glFlush before waiting for user input that depends on the generated image.

NOTES
glFlush can return at any time. It does not wait until the execution of all previously issued OpenGL commands is complete.

ERRORS
GL_INVALID_OPERATION is generated if glFlush is called between a call to glBegin and the corresponding call to glEnd.

SEE ALSO
"glFinish"


This still makes me think that it bypasses some kind of default timer. Instead of waiting to make sure the buffers recieve all incomeing commands it just pushes them through.

Monder Where have you read that glFlush() pauses program execution? Im VERY curious to know whether this is the case or not.
C_C(Enter witty/insightful/profound remark here...)
Quote:causing all issued commands to be executed as quickly as they are accepted by the actual rendering engine


There's the answer to why glFlush( ) will cause a wait. It causes all commands to be executed, which takes time. If glFlush( ) did not wait then all commands could not have be executed when glFlush( ) returns. glFlush is there so you can be sure all commands have been executed, thus it must wait while all commands execute before returning.

Quote:This still makes me think that it bypasses some kind of default timer. Instead of waiting to make sure the buffers recieve all incomeing commands it just pushes them through.


There is no timer, there's just buffers of commands that get executed when they can. glFlush just ensures that everything gets executed so you can then go do something that relies on everything having been executed.
This is probably a good thing, cause you dont want to swap the buffer, before you havnt rendered everyting, although, swapbuffer might wait until all commands have been processed (Pure speculation).
I'm pretty sure swapping the buffers does wait for all commands to finish, as I stated in one of my posts above. Otherwise you'd get half drawn scenes apearing on the screen.
Well in that case, I would use glFlush only when doing multiple render passes (Render to texture, or whatever), so that you are sure that your buffer contains the correct image, and let swapbuffer do its own thing.



glFlush just says "start rendering if you haven't already". It returns immediately. You could use this to let the gpu work in parallel with your cpu by using a game loop that looks like:

:begin
Draw current state
glFlush()
HandleInput
UpdateMotion
CollisionDetection
swap buffers
goto begin

glFinish is a blocking call, so it will not return until the driver has finished doing all the drawing. The only reason I use this function is for profiling purposes (otherwise you might get results that looks like:

draw terrain = 1 ms
draw creatures = 1 ms
swapbuffers = 35 ms

This topic is closed to new replies.

Advertisement