Is there any way to know if an OpenGL operation has completed?

OpenGL functions are defined as if they are working synchronously. But rendering functions (and others) will often be executed asynchronously using the GPU. OpenGL will effectively hide this: if you do something that requires the results of the operation (for example, reading from the framebuffer that was displayed), OpenGL will stop the CPU operations until the GPU reaches this point.

This is functional, but hardly ideal for performance, as the processor basically locks until the GPU is executed. Is there a way to determine if a particular operation has completed so that you know that the dependent operation will complete without so much CPU lock?

+7
asynchronous opengl
source share
1 answer

Yes there is. Assuming you have access to OpenGL 3.2+ or the ARB_sync extension.

If you want to know when a particular command has completed, you can insert a synchronization object into the "fence" command stream immediately after issuing this command. This is done using the glFenceSync function.

You must keep the pointer returned by this function. With this pointer in hand, you can ask if the fence is complete by checking if the fence is enabled using the glGetSync function. Like this:

 GLint isSignaled = 0; glGetSync(syncObj, GL_SYNC_STATUS, 1, NULL, &isSignaled); if(isSignaled == GL_SIGNALED) { //Prior commands have completed. } else { //Not done yet. } 

If you lack other things to work, you do not need to wait for the synchronization object to complete; you can just do whatever OpenGL process you want to do. This will call the processor unit for you.

Once you are done with the synchronization object, you should delete it. Use the glDeleteSync function to do this.

+6
source share

All Articles