Custom depth testing with deffered rendering

Hi all, I have a deferred rendering setup and I want to render debug meshes (such as an AAB surrounding the mesh) over my meshes. With a forward renderer I could just render these meshes in a subsequent pass as the depth information is preserved. But in a deferred rendering pass this is lost. I know I can probably store the depth in a g-buffer texture using the gl_fragDepth field but How do I use it in a shader to draw the wireframe meshes.

Also is there a better approach of drawing debug meshes when using deferred rendering ?

Thanks in advance

Why would deferred rendering be any different to forward rendering in this regard?

Because with deferred rendering I am calculating lighting in a different pass which just renders a quad calculating the lighting colors per fragment from all the screen space information in the g-buffers. Since it renders a quad on the screen as the final pass, the depth information about the objects will not be preserved and mostly depth testing would not work as expected.

… I don’t understand what you’re talking about.

The depth buffer is still there. It has the exact same values it had when you finished rendering the scene in the geometry pass. If you want to render debug meshes (presumably to the final image, since they’re probably not lit), you can just do that, using the depth buffer as-is.

The only way the depth buffer’s values won’t be preserved is if you don’t preserve them. Such as by clearing them, or doing some depth writes or some other operation that modifies the depth value.

The pass which renders the g-buffer will have a depth buffer, and that won’t go away unless you explicitly discard it.

If you want to render over the final colour buffer using the g-buffer’s depth buffer, you can create a FBO using those as the colour and depth attachments. If your lighting pass renders to the default framebuffer, you can’t mix textures/renderbuffers with the buffers of the default framebuffer, but you can copy the g-buffer’s depth buffer to the default framebuffer’s depth buffer with e.g. glBlitFrameBuffer.

Alternatively, if the overwrite pass only needs depth tests, and not depth writes (i.e. it tests against the geometry used for the g-buffer but not against other geometry in the ovewrite pass), you can perform that in the shader by binding the depth texture as a texture, sampling it, comparing it to gl_FragCoord.z, and executing a discard statement if the test fails.