The values that should be written to gl_FragDepth, should they be from 0.0 to 1.0 so that the fragment doesn’t get regected, or is it -1.0 to 1.0?
Also, does glDepthRange(u1, u2) have an effect?

gl_FragDepth = gl_FragCoord.z and gl_FragCoord is the fragment window coordinates, so gl_FragDepth should be between 0 and 1 and since glDepthRange defines this last range, this one affects the gl_FragDepth interval.
But glDepthRange clamps range to [0,1]

glDepthRange specifies the viewport depth transformation which does not get applied to gl_FragDepth. gl_FragDepth is only clamped to [0, 1].

I am curious, can I know why? The glsl spec says that the gl_FragDepth is written by the fixed function for depth and this one takes depth values in the depth range.

As I said, because glDepthRange only specifies the viewport depth transformation. The viewport transformation is applied to vertices, not fragments.

You can easily apply your own depth transformation in the fragment shader if you want to, but it’s good to have more flexibility.

thank you Xmas for this answer, but something is confusing me.
I know that viewport transformation is applied on vertices and if I am not mistaken vertices depth is inside the depth range after this transformation. Then triangles are rasterized and depth comparison is done on each fragment to discard not visible ones before doing per fragment operations. So I though that depth values at each fragment were depth values interpolated from triangle vertices (which are in depth range). But it seems I am mistaken here.

I think I see the source of confusion: gl_FragDepth is an output variable which can be written by the fragment shader to override the interpolated depth value.

The interpolated depth value is passed in to the fragment shader as gl_FragCoord.z, which is the value that gets used in the depth test if you don’t assign to gl_FragDepth. With gl_FragDepth the fragment shader can modify the depth value used, and the value written is not affected by the depth range.

By the way, the depth test conceptually takes place after the fragment shader and alpha test. But as long as alpha test is disabled and the fragment shader neither uses discard nor writes to gl_FragDepth, performing the depth test early yields identical results.

Ok! Now this is perfectly clear. :slight_smile:
Until your last post, I have not noticed the subtle thing about “depth comparison” and “fragment operation” order. I though that depth comparison was always before fragment operations as an optimization. This was the source of my confusion.

If you do not write data to gl_FragDepth, the hardware will automatic fill depth buffer.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.