I read here (Normal back buffer + render to depth texture in OpenGL (FBOs) - Stack Overflow) that it is impossible to mix backbuffer with a custom GL framebuffer. I ran into this problem today, but it appears that some sort of mixing does work.
I have a few passes in my engine. The first one is early-z, followed by a pass that writes down normals and depths. Early-z renders to the backbuffer, but normal-depth pass switches the render target (*), and disables writes to z-buffer. What is interesting is that normal-depth pass utilizies early-z to only shade pixels that are visible, but in theory that should not work as the backbuffer’s z-buffer is filled it, whereas I already have bound a new framebuffer object, which has not z-buffer render-buffer or texture bound at all.
On the other hand, if I skip the early-z pass, leaving only normal-depth that sets the render target, the further rendering screws up (note that right now, as there is no early-z, normal-depth pass has to write z values). So basically it appears that reading from the backbuffer’s Z is possible, but it is not possible to simultanously write to a framebuffer’s color, and backbuffer’s z. Is that correct?
I am aware that my description might be a bit enigmatic so if necessary, I can describe it in more detail and post more code.
(*) the function looks this:
void CRenderer::setRenderTarget(const CRenderTarget *renderTarget)
{
if (renderTarget == NULL)
{
glViewport(0, 0, CApplication::getScreenWidth(), CApplication::getScreenHeight());
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
else
{
glViewport(0, 0, renderTarget->width, renderTarget->height);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, offScreenFramebuffer);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTarget->texture, 0);
}
}