PDA

View Full Version : some experiments with gl_FragDepth



Vexator
12-26-2006, 03:30 AM
i have a simple shader with a color texture u_Texture0 and a depth texture rectangle u_DepthTexture.

first, i tried this:

gl_FragColor = texture2D( u_Texture0, Coordinates );which resulted in:

http://www.vexator.net/1.jpg

ok, fine, just what i expected.

then i tried:

gl_FragColor = texture2D( u_Texture0, Coordinates );
gl_FragDepth = gl_FragCoord.z; which should have had the same result, right? instead i got:

http://www.vexator.net/2.jpg

wtf? ok, then i tried to read depth from my depth texture rectangle:

gl_FragColor = texture2D( u_Texture0, Coordinates );
gl_FragDepth = texture2DRect( u_DepthTexture, gl_FragCoord.xy ).r;which resulted in:
http://www.vexator.net/3.jpg

my depth texture is fine, i assigned it to gl_FragColor and the output was the same as the depth texture. so what's wrong? btw my graphics board is a radeon 9800 pro -.-

k_szczech
12-27-2006, 06:59 AM
Your description is a bit unclear. Is this what you're trying to do:
1. Render 3D scene
2. copy color & depth to textures
3. put scene to screen
4. overlay some geometry on top of it
?

Please describe what you currently do. Just single line from a shader is not enough if you don't know where this shader is used.

Your problems probably come from comparing 24-bit depth buffer with 16-bit depth texture, depth-texture filtering or shaders that are not position invariant. Could be something else. At this point I cannot tell you more.

Vexator
12-27-2006, 08:48 AM
1) depth and ambience pass is rendered
2) depth is copied to a texture rectangle
3) lighting passes are rendered with a simple shader. the only interesting lines are the ones i posted above.

the depth texture rectangle is to have 24bit depth, at least i create it like this:


glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT24, m_kSize.x, m_kSize.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0 );filtering is set to GL_NEAREST for both min & max.

thanks :)

k_szczech
12-27-2006, 02:01 PM
the only interesting lines are the ones i posted aboveNot really - your problem is probably in the vertex shader - do you use ftransform() ?

And does your second pass render any geometry or is it just a fullscreen quad? Do you really need depth test in second pass?

If you need more information about z-buffer storage, then have a look at this page:
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

def
12-28-2006, 05:23 AM
Originally posted by Vexator:
glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT24, m_kSize.x, m_kSize.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0 );You will need to use GL_INTEGER, GL_UNSIGNED_BYTE will convert the depth values back to 8bit. I think.

k_szczech
12-28-2006, 09:35 AM
You will need to use GL_INTEGER, GL_UNSIGNED_BYTE will convert the depth values back to 8bit. I think.No, last 3 parameters only specify what kind of data you pass from CPU. And since last parameter is NULL texture image is not initialized. So actually GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE don't matter at all.

Vexator
12-28-2006, 09:37 AM
Not really - your problem is probably in the vertex shader - do you use ftransform() ?ja.


And does your second pass render any geometry or is it just a fullscreen quad? Do you really need depth test in second pass?i render the same geometry as in the initial pass. depth mask is disabled for the lighting passes.


You will need to use GL_INTEGER, GL_UNSIGNED_BYTE will convert the depth values back to 8bit.ok, changed it. didn't help, though..

Komat
12-28-2006, 12:20 PM
The specification states that:
All shaders that either conditionally or unconditionally copy the input gl_FragCoord.z to the output gl_FragDepth are depth-invariant with respect to each other, for those fragments where this copy actually is done. Fragment shader that does not write to gl_FragDepth is depth-invariant with fixed function.

The specification does not guarantee depth-invariance if one shader writes to gl_FragDepth and the second does not.

Vexator
12-28-2006, 12:41 PM
mmh.. i only use two shaders, and the one i'm not writing to gl_depth is the one which is active when the depth buffer is written to. so is there a way for me to solve this?

Komat
12-28-2006, 12:53 PM
Originally posted by Vexator:
mmh.. i only use two shaders, and the one i'm not writing to gl_depth is the one which is active when the depth buffer is written to. so is there a way for me to solve this? Write the depth in both of them or in none from them.

Why do you need to write the depth? Writing the depth from shader will kill various early depth test optimizations even if the depth buffer writes are disabled.