Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "add" Depth information to the main frame buffer

Tags:

opengl

glsl

Let's say I have this scene enter image description here

And I want to add depth information from a custom made fragment shader.

Now the intuitive thing to do would be to draw a quad over my teapot without depth test enabled but with glDepthMask( 1 ) and glColorMask( 0, 0, 0, 0 ). Write some fragments gl_FragDepth and discard some other fragments.

if ( gl_FragCoord.x < 100 )
    gl_FragDepth = 0.1;
else
    discard;

For some reason, on a NVidia Quadro 600 and K5000 it works as expected but on a NVidia K3000M and a Firepro(dont't remember which one), all the area covered by my discarded fragments is given the depth value of the quad.

Can't I leave the discarded fragments depth values unmodified?

EDIT I have found a solution to my problem. It turns out that as Andon M. Coleman and Matt Fishman pointed out, I have early_fragment_test enabled but not because I enabled it, but because I use imageStore, imageLoad.

With the little time I had to address the problem, I simply copied the content of my current depth buffer just before the "add depth pass" to a texture. Assigned it to a uniform sampler2D. And this is the code in my shader:

if ( gl_FragCoord.x < 100 )
    gl_FragDepth = 0.1;
else
{
    gl_FragDepth = texture( depthTex, gl_PointCoord ).r;
    color = vec4(0.0,0.0,0.0,0.0);
    return;
}

This writes a completely transparent pixel with an unchanged z value.

like image 630
Jean-Simon Brochu Avatar asked Oct 20 '22 10:10

Jean-Simon Brochu


1 Answers

Posting a separate answer, because I found some new info in the OpenGL spec:

If early fragment tests are enabled, any depth value computed by the fragment shader has no effect. Additionally, the depth buffer, stencil buffer, and occlusion query sample counts may be updated even for fragments or samples that would be discarded after fragment shader execution due to per-fragment operations such as alpha-to-coverage or alpha tests.

Do you have early fragment testing enabled? More info: Early Fragment Test

like image 148
Matt Fichman Avatar answered Oct 24 '22 00:10

Matt Fichman