Blur the depth buffer

I have a shaderthat does a two pass Gaussian blur on a GL_RED texture. Now I want to blur a depth buffer. Is it possible to use a FBO with the output to a depth texture in GL_COLOR_ATTACHMENT0? It didn’t work when I tried.

If this is not possible, do I have to make another blur filter, that blurs the depth instead of the pixel color?

The purpose is to implement Exponential Shadow Maps (ESM).

A follow-up question: I need to allocate two depth buffers, to have the blurring shader alternating outputs to use. This is a little costly, but I don’t see any way around this?

To your first question - create a texture buffer and attach it as depth buffer


glGenTextures(1, &c_RenderDepthTexture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, c_RenderDepthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, c_Samples, GL_DEPTH24_STENCIL8, c_Width, c_Height, GL_FALSE);                
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, c_RenderDepthTexture, 0);

Now you have a texture to use later

To your second question - no but you can detach one and attach another

I don’t understand. If I attach it as a depth buffer, I can’t use the current blur filter that blurs a color buffer, can I?

You can’t use it in the pass that you write to it - it is not a read/write buffer. You do a depth render pass then attach it as an extra texture for a colour pass.

I found a solution to this. The objective was to use the same blurring shader for color textures and depth buffers. To extend the shader to also allow blurring of depth buffers, I simply added the following line in the end:

gl_FragDepth = fragColor.r;

The shader still uses one input texture, which can be either a color bitmap or a depth texture. The FBO output has to be configured so as to either write to a GL_COLOR_ATTACHMENT, or write to the depth map. The extra line doesn’t seem to add any performance penalty.

Sorry I misunderstood. I thought you wanted to read an arbituary part of the depth buffer in the shader.

either write to a GL_COLOR_ATTACHMENT, or write to the depth map

You must write something all all attachments that exist in a fragment shader or you will get unpredictable results.