PDA

View Full Version : GLSL & depth texture



dletozeun
05-02-2008, 11:30 AM
Hello,

I need to retrieve the value of a texel in a depth map with a glsl fragment shader which coordinates are calculated in a vertex shader.
I know how to obtain the depth texture from the depth buffer using GL_DEPTH_COMPONENT data format. Usually this kind of texture is used for shadow mapping and is bound to a sampler2DShadow variable in order to be used by functions like shadow2D, shadow2DProj...

But In my Application, I need to retrieve the depth value, not just 0.0 or 1.0 given by the last built-in functions.

How can I do that? It seems that sampler2DShader is the only data type that can handle depth textures...

If I use a rgb texture to store depth (using a shader) I would loss a lot of resolution... Is it possible to split the Z value in the rgb components in a shader?

thanks a lot.

Komat
05-02-2008, 12:32 PM
You can get depth from the GL_DEPTH_COMPONENT texture. To do that you need to disable the depth comparison mode on that texture (by setting the GL_TEXTURE_COMPARE_MODE to GL_NONE) and use the ordinary sampler2D.

dletozeun
05-02-2008, 02:33 PM
Thank you Komat for your reply.

But even if I disable the depth comparison mode (I don't see why), how can I access to depth texture values? Using texture2D function and reading one of the component of the returned vec4 value?

dletozeun
05-03-2008, 03:08 AM
I just add one more thing:

If I use texture2D in my fragment shader to access a depth texture bound to a sampler2D sampler, does it guarantee that the precision of the returned depth value is still the depth buffer precision (16, 24 or even 32 bits), or is it 8bits precision?

Thanks

dletozeun
05-03-2008, 03:43 PM
I answer to myself:

Finally I decided to not use a depth map which pixel format is GL_DEPTH_COMPONENT but RGB and with a shader I copy the fragments Z value to this RGB texture. To save 16, 24 or even 32 bit Zbuffer resolution, each r, g and b channels ( plus the a channel for a 32 bits Zbuffer) store a 8 bit part of the Z value.

Komat
05-04-2008, 05:22 AM
But even if I disable the depth comparison mode (I don't see why),how can I access to depth texture values? Using texture2D function and reading one of the component of the returned vec4 value?

The comparison mode must be disabled because otherwise the result of using the texture2D function on depth texture is undefined. And yes, you use that value by reading one from components of the vec4 depending on the GL_DEPTH_TEXTURE_MODE mode enabled on that texture.



If I use texture2D in my fragment shader to access a depth texture bound to a sampler2D sampler, does it guarantee that the precision of the returned depth value is still the depth buffer precision (16, 24 or even 32 bits), or is it 8bits precision?

I do not know if there is any precision guarantee however I would expect that it matches the precision of the buffer.

dletozeun
05-04-2008, 10:14 AM
Ok thank you Komat.
About the precision, if I store the depth value in just one component (or if I duplicate this value on all components) the depth value precision preserved in a fixed point texture. I should use a floating point texture or like I said before, store the 24bit s depth value on rgb components using bit shifting.

khamoon
08-27-2008, 07:34 AM
I had problems with depth texture precision when sampled in fragment program without percentage-closer filtering. The problems occured on gf6800, on gf8600 it all worked fine. Using GL_NEAREST on both min & mag filters helped, now i get full 24bit fixed point precision of depth texture and thre's no need for channel split etc.