Difference between glFlush and glFinish

Asked

Viewed 608 times

2

What is the practical difference between glFlush and glFinish commands in the context of Opengl ES 2.0?

I have read the documentation of the two functions glFlush and glFinish, and I haven’t been able to determine when I must use one instead of the other.

I know that a simple code works correctly without using any of these functions, which is exactly why I would like code examples where each of these functions proves to be indispensable.

1 answer

3


Opengl commands are not executed immediately. Instead, they are subjected to a buffer command which is then fed to the hardware. The commands glFlush() and glFinish() are both used to force the submission of buffer command to the hardware for execution.


glFlush

It serves to send to the hardware all commands issued so far, thus allowing the drivers GL surrender without great delays, also ensuring that in a finite period all tasks will be completed.

This command makes return immediately upon completion of sending the commands, does not wait for the surrender nor 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), ensure that the GPU has all commands for ball #1 before receiving anything concerning ball #2.

Note: There is no guarantee that the GPU has finished the #1 ball or even started the surrender of the same when the glFlush() is called. We only have the guarantee that the GPU has everything you need for the surrender of the same.


glFinish

Is equal to glFlush(), but will make the execution of the program wait until it is all drawn down to the last pixel, until the GPU have nothing 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 the surrender what 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 charged before the render start on the second thread.

Note: It is also important to know that when we call glFinish, we are somewhat losing the parallelism that the GPU and CPU can achieve, since as glFinish has the characteristic to force the wait until everything is rendered, in the thread in which it is called, the GPU is working in full but the CPU is waiting for the surrender while I could be advancing other work. With this I intend to alert to the careful use of glFinish.

  • Thank you so much! For now I’m using glFinish(), but I may be losing performance. I will test with glFlush() :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.