I have spent a lot of time trying to solve this problem and searching for answers. But I can't figure it out.
I am trying to preserve the depth buffer while rendering two images to texture. I have tried two different solutions, which both works on desktop, but not on android (opengl-es 2.0, libGDX). Both solutions also produce exact same results. I have also tried two different devices. Samsung TAB 2, and HTC One M7. Same results.
First solution: Create two FBO's, and share the depth buffer attachment between them.
Create FBO1, with texture 1 as color attachment and a depth attachment. Create FBO2, with texture 2 as color attachment, and depth attachment with same handle as depth attachment on FBO1.
I then bind FBO1, clear the depth and color, render, unbind FBO1, bind FBO2, clear color, and render, unbind FBO2.
Second solution: Create one FBO, and change color attachments.
Bind fbo, attach Texture1 to the only available GL_COLOR_ATTACHMENT0. clear color and depth, render, attach Texture2 to the only available GL_COLOR_ATTACHMENT0, clear color, render, unbind fbo.
in both solutions I then draw out both passes as fullscreen quads. First pass first, second pass second.
Both solutions produce the exact same results, which are correct for desktop and wrong for android.
Edit: I now have 10 in reputation, and and am able to post images
Clarification: The "greenish room" is rendered in the first pass/texture, while the "colorful spheres" is rendered in the second pass/texture.
First picture, desktop:
[render on desktop: The depth buffer is preserved throughout both passes. Parts of the second pass ends up behind parts of the first pass. This is correct]
Second picture, device:
[Render on android: The depth is correct for each individual pass, but the second pass gets rendered always on top of the first pass, even though parts of the second pass is behind parts of the first pass (and therefore should have been culled by the depth test), This leads me to believe that the depth buffer gets cleared between passes.]
So why does the depth buffer get cleared between passes on mobile devices, but not on desktop, and what is the solution?