How to copy depth buffer to a texture on the GPU?

Anon picture Anon · Jun 14, 2011 · Viewed 10.4k times · Source

I want to get the current depth buffer to a texture, to access it in a shader. For various reasons I can't do a separate depth pass, but would need to copy the already-rendered depth.

glReadPixels would involve the CPU and potentially kill performance, and as far as I know glBlitFramebuffer can't blit depth-to-color, only depth-to-depth.

How to do this on the GPU?

Answer

Nicolas Lefebvre picture Nicolas Lefebvre · Jun 14, 2011

The modern way of doing this would be to use a FBO. Attach a color and depth texture to it, render, then disable the FBO and use the textures as inputs to a shader that will render to the default framebuffer.

All the details you need about FBO can be found here.