Render FBO to screen taking into account Depth

Target: OpenGL ES 3.0

Suppose my default Framebuffer (the screen) already contains part of my scene rendered; its color and depth buffers contain valid data.

Now suppose I have another part of the scene rendered to an intermediate FBO ‘fbo’ already. Its color data is kept in texture ‘mColor’, which is attached to fbo.COLOR0 attachment, and its depth is kept in another texture ‘mDepth’, which is attached to fbo.DEPTH attachment.

Now I would like to take mColor and render it to the Screen, with DEPTH test enabled and taking into account the depth from the ‘mDepth’ texture.

Essentially it looks like when we render the Quad textured with mColor to the screen, we need to compare the Screen’s depth buffer not with the depth computed from linearily interpolating the 4 verticaes of the Quad, but with depth from the ‘mDepth’ texture.

How to do that?

Search for “depth sprite”.