# FBO blit depth buffer to screen?

I have an FBO in a deferred 4.3 OpenGL renderer, in which I setup the depth buffer of that FBO like this:

GLCALL(glGenRenderbuffers(1, &mDepthbuffer));
GLCALL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthbuffer));
GLCALL(glRenderbufferStorageMultisample(GL_RENDERBUFFER, 0, GL_DEPTH32F_STENCIL8, windowWidth, windowHeight));
GLCALL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthbuffer));
GLCALL(glBindRenderbuffer(GL_RENDERBUFFER, 0));


Normally when I debug I can output the color attachments to screen easily, like this for normals:

GLCALL(glReadBuffer(GBuffer::GBUFFER_COLOR_ATTACHMENT_NORMAL));
GLCALL(glBlitFramebuffer(0, 0, mWindowWidth, mWindowHeight, 0, 0, mWindowWidth, mWindowHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR));


But how can I do the same for the depth buffer contents, as it is not a color buffer?

First of all if you tried to do exactly that it would not work because blitting the depth / stencil buffer using GL_LINEAR is undefined. It literally lists this very situation as the first possible error on the manual page for glBlitFramebuffer (...):

Name

glBlitFramebuffer — copy a block of pixels from the read framebuffer to the draw framebuffer

...

Errors

GL_INVALID_OPERATION is generated if mask contains any of the GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT and filter is not GL_NEAREST.

More importantly, however, it is generally quicker to draw a textured quad than to do a blit from a read buffer to a draw buffer. It seems almost counter-intuitive; after all, the only reason glBlitFramebuffer (...) was created was to copy data from one framebuffer to another. But if you want the best performance just stick to textured quads.

The textured quad approach will also allow you to do linear interpolation of the depth buffer, which is a limitation you cannot work around if you do a blit.

The only real reason you would ever consider using glBlitFramebuffer (...) is if you want to do MSAA resolve of a multi-sampled renderbuffer. This is the least painful way of accomplishing that particular task, particularly if you are dealing with Shader Model 4.0 (DX10) hardware, which only supports multi-sampled color textures. You can implement MSAA resolve in shaders using texelFetch (...) but unless you have Shader Model 4.1 (DX10.1) hardware you cannot use multi-sampled depth textures.

• if I glBlitFramebuffer with GL_DEPTH_BUFFER_BIT, does the depth get written to the draw FBOs depth buffer or color buffer? I assume its to the depth buffer? – KaiserJohaan Jan 5 '14 at 13:19
• @KaiserJohaan: That is correct, if you blit the framebuffer, it will copy the contents from the corresponding read buffer into the draw buffer. In this case, the depth buffer. It will not copy the depth buffer to the draw buffer, you need to use a textured quad and sample the depth texture in order to display the depth as a color. – Andon M. Coleman Jan 5 '14 at 19:50