OpenGL commands do not run immediately. Instead, they are submitted to a buffer command that is then fed to the hardware. The glFlush()
and glFinish()
commands are both used to force the command buffer submission to the hardware for execution.
glFlush
Serves to send to the hardware all the commands issued so far, thus allowing the GL GL to start rendering without major delays, also ensuring that in a finite period all tasks will be completed.
This command makes return
immediately after completion of sending the commands, does not wait for render , or blocks the application.
Example
Imagine that you are drawing an animation with balls, appearing one and then another and then another, etc ..., calling glFlush
after each object
(ball), guarantors that the GPU has all the commands referring to ball # 1 before receiving whatever is referring to ball # 2.
Note: There is no guarantee that GPU has completed ball # 1 or not even started the render of it when glFlush()
is called. We only have the guarantee that GPU has everything you need for render of it.
glFinish
It is equal to glFlush()
, but will cause the program execution to wait until everything is drawn to the last pixel, until GPU does not have anything else to do.
This command will only do return
when all commands sent before it are completed.
Example
Somehow useful when there is interaction with the operating system and / or user and this interaction depends on render that is being done.
Another example is the case of resources shared between threads
where one is loading a texture whose same will be rendered in another thread
, where the use of glFinish
will ensure that the texture is fully loaded before render
start on second thread
.
Note: It should also be noted that when we call glFinish
, we are somehow losing the parallelism that GPU and CPU can achieve, since as glFinish
has by characteristic to force the wait until that everything is rendered, in the thread
in which it is called, the GPU is working in full but the CPU is waiting for render while he could be advancing other works. With this I intend to alert for the careful use of glFinish
.