Preserving back buffer content after eglSwapBuffers
android, c++, double-buffering, egl, opengl-es
Solution
Your general approach is correct. Using `glBlitFramebuffer()` is likely to be more helpful, as `glCopyPixels` just copies some sub-rectangle of the buffer to anouth position in the same buffer.
However, I can think of a potentially better approach, at least if `OES_framebuffer_object` is available:
- draw to texture or a user-defined renderbuffer
- render texture to BACK buffer/blit renderbuffer to BACK buffer
- swap
- update texture/renderbuffer
- render texture to BACK buffer/blit renderbuffer to BACK buffer
- swap
- [... and so on ...]
That way you don't have a copy/update/copy/swap cycle with 2 copies per frame, but only update/copy/swap with a single extra copy.
Problem
I'm implementing in Android and native C++ a scene drawing for android using EGL 1.1. Currently using Android's `glSurfaceView` - which allows me to draw to a back buffer which is displayed at the end of "onDrawFrame" - when the back buffer and the front buffer are swapped. My problem is this: I need to be able to display the back buffer and continue writing as if I havn't swapped. The reason behind this demand is that the scene is very large, and building it each frame is not possible, neither is waiting for the end of the drawing - since the user will have to wait for too long. In other words - I need to build the scene incrementally. At a certain point during the rendering, I decide it's time and I call `eglSwapBuffers` which displays the drawn content from the back buffer, but when I continue writing obviously I'm writing to the 'former-front-buffer' which is out of sync.. (not containing the things I've drawn so far). As far as I see my only option is to copy the back buffer before swapping. pseudo: - Draw to back buffer - Copy Back Buffer to temp Buffer - Swap - Copy temp buffer to (the new) back buffer - Draw more things to back buffer - And so on... Is there a way to do steps 2,4? - Is `glCopyPixels` useful in this case? example? - Is `glBlitFramebuffer`? Or am I approaching this all wrong? Things I already did : I tried setting `EGL_SWAP_BEHAVIOR` to `EGL_BUFFER_PRESERVED` but it only seems to work on certain devices (as described in the khronos notes) : Some surfaces allow applications to control whether or not the color buffer contents are preserved Re-rendering the scene in each frame - not possible. I've read many times that this is recommended.