FrameBuffer mixing with BackBuffer

I read here (http://stackoverflow.com/questions/5279123/normal-back-buffer-render-to-depth-texture-in-opengl-fbos) 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);
  }
}

I don’t know of a way to tell OpenGL that you want to use the backbuffer and another buffer simultaneously. I am not sure what happens if you enable depth testing without a z-buffer attached.

you just get no depth testing

Then why do I get depth testing working correctly when I switch to an offscreen buffer with only color buffer attached? Moreover, z-values are correctly read for the backbuffer’s z-buffer.

It sounds like the driver may be attaching the back-buffer’s depth buffer but since this is not standard behaviour you can’t assume it will continue to work.