Markus A.
Markus A.

Reputation: 12762

Incrementally updating screen buffer for OpenGL ES

When the render function of an OpenGL app is called, can I rely on the fact that the screen-buffer still contains whatever I drew into it during the last call to my render function that was working on that buffer?

In other words, would it be possible to draw only incremental updates to the buffer or do I need to redraw the entire screen every time, just in case?

I realize that the logic required to make such an approach work would be quite involved as there are multiple screen buffers, so the update needs to be incremental not relative to the last call to the render function, but the last call that actually drew into this particular screen buffer. But depending on the type of screen updates and the required performance, this might still be a worthwhile thing to investigate. So, any answers to the effect of "don't do it!" should be based on actual reliability issues with this idea like possible random buffer corruption and not just "it's way too hard to debug"...

Upvotes: 1

Views: 443

Answers (2)

Reto Koradi
Reto Koradi

Reputation: 54642

No, you can't rely on the previous content still being there. The exact behavior is very platform and even device dependent. Or even worse, it could change on the same device depending on conditions that are partly outside your control.

On some platforms, there are ways to preserve the previous buffer content. For example on Android, as already mentioned in another answer, there is a EGL_BUFFER_PRESERVED attribute on the surface. However, this is not supported on all devices. Unless you're targeting a specific device for your software, you'll have to check for the availability of the feature, and use a different approach if the feature is not present. I explained this in some more detail in a related answer here: Fast Screen Flicker while NOT drawing on Android OpenGL. Another limitation is that even if it the feature is present, it will only preserve the color buffer. If your rendering relies on a depth or stencil buffer that also needs to be preserved, this will not help you.

If your rendering is really so expensive that incremental drawing is necessary, the only portable solution is that you render to an FBO, and copy the FBO content to the default framebuffer on each redraw.

Upvotes: 2

genpfault
genpfault

Reputation: 52157

Depends on what you have EGL_SWAP_BEHAVIOR set to on your surface:

eglSwapBuffers():

... The contents of the color buffer are left unchanged if the value of the EGL_SWAP_BEHAVIOR attribute of surface is EGL_BUFFER_PRESERVED, and are undefined if the value is EGL_BUFFER_DESTROYED. The value of EGL_SWAP_BEHAVIOR can be set for some surfaces using eglSurfaceAttrib.

eglSurfaceAttrib notes that:

The initial value of EGL_SWAP_BEHAVIOR is chosen by the implementation.

...so make sure you actually set the value you need and don't rely on the (unreliable) default.

Upvotes: 1

Related Questions