Advertisement

How to perform depth test in pixel shader

Started by September 10, 2009 02:09 AM
10 comments, last by parmar 15 years, 2 months ago
Hi Guys, I wanted to know how we can perform depth test in pixel shader for knowledge purpose.I am using openGL ES 2.0 and GLSL 1.0.I have tried to search on google also but i didnt get any much info on this topic.Please give me some link or if any document is there. Regards, Parmar.
Doing depth testing yourself would require reading what is already in the depth buffer and reading the depth buffer is not possible from a pixel shader.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Advertisement
Don't think you can, pixels don't get to the pixel shader if they fail the depth test.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Thanks V-man for your reply.As usual you replied for my question.

I think i could not formed the proper sentence.
How i will get the depth value of a pixel in
fragment shader.And can we write our own implementation
for depth test in vertex shader.
Regards,
Parmar.
Quote: Original post by Nanoha
Don't think you can, pixels don't get to the pixel shader if they fail the depth test.


Wrong. If you output your own depth value, the depth test happens after the pixel shader stage. Read about gl_FragDepth.

Quote: Original post by parmar
How i will get the depth value of a pixel in
fragment shader.And can we write our own implementation
for depth test in vertex shader.


You could do render to a depth texture in pass 1, then in the next pass, read that texture and do your own depth testing. Normally, you do this in a pixel shader.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Thanks V-man,

@V-man Is any document or sample is there for this concept.
so I can go through it.I am a regular reader of this
community but i didnt get much info on this concept.


@Nanoha thanks for your reply.

Advertisement
There is this extension
examples are on that page
http://www.opengl.org/wiki/GL_EXT_framebuffer_object

There is the GL 3.0 version
http://www.opengl.org/wiki/Framebuffer_Objects
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

I have a one doubt about depth test.
After vertex sahder traingle will rasterize,and it will get color and
depth value of per fragemnt.So why we dont have the depth test there only.
Even some fargment is occuleded we are accesing the per fragemnt color
and texture fetch in pixel shader.Can any one suggest.

Parmar.
Search for early Z pass, pre z pass, etc. you generally turn off color writes, and only write depth, so you dont bother with textures in an early z pass, so there is no color value being written.
Couldn't this depth test (the rendered to texture thing) used to draw decals on surfaces?

simply:
if(fabs(frag_depth-texture_depth(x,y))> tolerance )
discard fragment

No need of clipping, just draw it

This topic is closed to new replies.

Advertisement