PDA

View Full Version : Blur the depth buffer



Kopelrativ
02-12-2013, 11:21 PM
I have a shader (https://github.com/larspensjo/ephenation-client/blob/ec4f15bb1eaea904925a0ad01897188b4ba5cc02/Source/shaders/gaussblur.glsl)that 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 (https://docs.google.com/viewer?url=http%3A%2F%2Fresearch.edm.uhasselt.be%2 Ftmertens%2Fpapers%2Fgi_08_esm.pdf) (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?

tonyo_au
02-12-2013, 11:54 PM
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

Kopelrativ
02-13-2013, 01:42 AM
To your first question - create a texture buffer and attach it as depth buffer

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?

tonyo_au
02-13-2013, 02:23 PM
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.

Kopelrativ
02-19-2013, 04:56 AM
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 (https://github.com/larspensjo/ephenation-client/blob/761dc628032603ba8a5b9054c0844e0c9df21235/Source/shaders/gaussblur.glsl#L64):

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.

tonyo_au
02-25-2013, 08:26 PM
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.