problems with sharing depth buffer in FBO

Hi guys,
I encounter a problem when using FBO in deferred shading system, my rendering pipeline is like this:

  1. render the scene into GBuffers using MRT;
  2. calculate sky lighting in screen space and output to a color texture;
  3. output color texture to main frame buffer;
    The problem is I wanna step 2 share depth buffer with step 1, so the calculation in step 2 only happen on pixels that is useful.
    I tried create render buffer in step 1 and bind it in the FBO create in step 2 (using glFramebufferRenderbuffer), but the result is not as excepted, the depth test is not correct in step 2 and no pixel be drawn.
    Anyone has done this before? or is any other way to share depth buffers among FBOs or FBO & Main FrameBuffer? any suggestion will be appreciated.

You don’t need a renderbuffer for that. Create a depth texture and simply attach it to both FBOs. That’s what I’m doing in my light pre-pass renderer and it works well(actually 3 FBOs share the same depth attachmant in my implementation).

The only thing to make sure is that the order of operations is well defined to rule out side effects.

Problem found! I forget to change the depthfunc after step 2 rendering, so another frame begin with the wrong depth func and nothing draw at the beginning.
Both renderbuffer and depth texture could be used among FBOs, and thanks thokra.