André Puel
André Puel

Reputation: 9209

Is glDrawElements synchronized on back buffer?

If I call glDrawElements with the draw target being the back buffer, and then I call glReadPixels, is it guaranteed that I will read what was drew?

In other word, is glDrawElements a blocking call?

Note: I am observing an weird issue here that may be caused by glDrawElements not being blocking...

Upvotes: 2

Views: 1089

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474336

In other word, is glDrawElements a blocking call?

That's not how OpenGL works.

The OpenGL memory model is built on the "as if" rule. Certain exceptions aside, everything in OpenGL will function as if all of the commands you issued have already completed. In effect, everything will work as if every command blocked until it completed.

However, this does not mean that the OpenGL implementation actually works this way. It just has to do everything to make it appear to work that way.

Therefore, glDrawElements is generally not a blocking call; however, glReadPixels (when reading to client memory) is a blocking call. Because the results of a pixel transfer directly to client memory must be available when glReadPixels has returned, the implementation must check to see if there are any outstanding rendering commands going to the framebuffer being read from. If there are, then it must block until those rendering commands have completed. Then it can execute the read and store the data in your client memory.

If you were reading to a buffer object, there would be no need for glReadPixels to block. No memory accessible to the client will be modified by the function, since you're reading into a buffer object. So the driver can issue the readback asynchronously. However, if you issue some command that depends on the contents of this buffer (like mapping it for reading or using glGetBufferSubData), then the OpenGL implementation must stall until the reading operation is done.

In short, OpenGL tries to delay blocking for as long as possible. Your job, to ensure performance, is to help OpenGL to do so by not forcing an implicit synchronization unless absolutely necessary. Sync objects can help with this.

Upvotes: 8

Related Questions