• Create Account

## Forcing early Z, which extension to use?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1ic0de  Members

955
Like
0Likes
Like

Posted 09 July 2013 - 06:55 PM

As far as my research has shown that there are two different extensions that can be used to force an early Z test in OpenGL 3.0. these are GL_ARB_conservative_depth and GL_ARB_shader_image_load_store. According to the spec they can be used to force early Z in glsl like so:

GL_ARB_conservative_depth:

layout(depth_unchanged) out float gl_FragDepth;


layout(early_fragment_tests) in;


My question is do these have the exact same effect? if so can I use them interchangeably, if not which one should I use?

you know you program too much when you start ending sentences with semicolons;

2052
Like
1Likes
Like

Posted 09 July 2013 - 09:14 PM

Does not load like image_load_store has anything to do with early depth test. So no, you should use the conservative depth.

Edited by dpadam450, 09 July 2013 - 09:14 PM.

### #3ic0de  Members

955
Like
1Likes
Like

Posted 09 July 2013 - 09:25 PM

Does not load like image_load_store has anything to do with early depth test. So no, you should use the conservative depth.

Yeah I know that the purpose of the extension is not specifically for early z tests but it can be used to enable them as I read here:

http://www.opengl.org/wiki/Early_Depth_Test#Explicit_specification

What I was hoping was that cards that didn't support GL_ARB_conservative_depth might support GL_ARB_shader_image_load_store to be used as a fallback or vice versa.

you know you program too much when you start ending sentences with semicolons;

2052
Like
0Likes
Like

Posted 10 July 2013 - 12:32 AM

I don't know. I just read that and nothing there suggests anything you are talking about. It is talking about how one thing affects the other. It says nothing about how textures (image load store) will effect depth testing. It does however say how depth testing will affect the image load store.

FYI from that page: "Thus the first restriction on early depth tests is that they cannot happen if the fragment shader writes gl_FragDepth​. If the fragment shader modifies the depth, then the depth test must wait until after the fragment shader executes."

In GL 2.0 (and it seems it has carried on to newer versions). If you don't write the depth in the shader, early z-cull and depth writing already takes place.

### #5ic0de  Members

955
Like
0Likes
Like

Posted 10 July 2013 - 09:44 AM

FYI from that page: "Thus the first restriction on early depth tests is that they cannot happen if the fragment shader writes gl_FragDepth​. If the fragment shader modifies the depth, then the depth test must wait until after the fragment shader executes."

I'm sorry but you clearly didn't read that entire article, the use of that syntax for forcing early Z requires GL_ARB_shader_image_load_store:

More recent hardware can force early depth tests, using a special fragment shader layout qualifier:

layout(early_fragment_tests) in;

This will also perform early stencil tests.

...

This feature exists to ensure proper behavior when using Image Load Store or other incoherent memory writing.

Its mentioned in the spec too:

An explicit control is provided to allow fragment shaders to enable early

fragment tests. If the fragment shader specifies the
"early_fragment_tests" layout qualifier, the per-fragment tests described
in Section 3.X will be performed prior to fragment shader execution.
Otherwise, they will be performed after fragment shader execution.

Edited by ic0de, 10 July 2013 - 09:45 AM.

you know you program too much when you start ending sentences with semicolons;

### #6Sik_the_hedgehog  Members

2951
Like
1Likes
Like

Posted 10 July 2013 - 10:33 AM

In GL 2.0 (and it seems it has carried on to newer versions). If you don't write the depth in the shader, early z-cull and depth writing already takes place.

Yep.

I don't think OpenGL 2 even has the notion of early depth test at all (much like how it doesn't specify the exact algorithm for defining the shape of triangles). It was an optimization done by the hardware and as long as it gave the expected results it could do anything it wanted, so early depth tests worked by default simply because there was nothing against it. I imagine that disabling it if you modify the depth in a pixel shader has to do with caching (it invalidates the value in the cache).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #7marcClintDion  Members

435
Like
0Likes
Like

Posted 10 July 2013 - 03:12 PM

I don't think OpenGL 2 even has the notion of early depth test at all (much like how it doesn't specify the exact algorithm for defining the shape of triangles). It was an optimization done by the hardware and as long as it gave the expected results it could do anything it wanted, so early depth tests worked by default simply because there was nothing against it. I imagine that disabling it if you modify the depth in a pixel shader has to do with caching (it invalidates the value in the cache)..

Using blending will also disable this hardware optimization, for PowerVR at least.  They call this feature "Tile Based Deferred Rendering" in case anyone wants to look it up.  Those little machines can handle a lot until you turn on blending, and presumably pixel shader depth writes.  Once you do this they slow to a crawl.

I haven't gone to any trouble to see if the other embedded system manufacturers have similar schemes running under the hood.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.

2052
Like
0Likes
Like

Posted 10 July 2013 - 08:37 PM

Well as this article (which I didn't initially read) suggests, some cards support this and they use the word "explicitly" which implies that there is an "implicit" case.
ATI and NVIDIA are going to be supporting this early optimization. I don't know anything that suggests otherwise and have read some internal docs.

This extension also provides the capability to explicitly enable "early"
per-fragment tests, where operations like depth and stencil testing are
performed prior to fragment shader execution.  In unextended OpenGL,
fragment shaders never have any side effects and implementations can
sometimes perform per-fragment tests and discard some fragments prior to
executing the fragment shader.

2052
Like
0Likes
Like

Posted 10 July 2013 - 08:42 PM

http://gamedev.stackexchange.com/questions/16588/computing-gl-fragdepth

It has been existing for a long time. Also its not just early z-cull, its hierarchical early z-cull.  Look up hierarchical occlusion culling. Graphics cards support this on a per-triangle level, which would not be possible if the shader executed first.

I believe the extension is explicitly able to perform the depth test by reading the depth buffer. Look up "discard".  You can discard any fragment in GL, to explicitly discard if (z < depthBuffer.z), you were not allowed direct access to the depth buffer. I don't know but am assuming that you are now allowed to read it. This may only be if you are using an FBO though........

2052
Like
0Likes
Like

Posted 10 July 2013 - 08:46 PM

Using blending will also disable this hardware optimization, for PowerVR at least.  They call this feature "Tile Based Deferred Rendering" in case anyone wants to look it up.  Those little machines can handle a lot until you turn on blending, and presumably pixel shader depth writes.  Once you do this they slow to a crawl.

Is this tested? Have you tried enabling and then disabling depth test on enough blended fragments to test the performance is actually different? Seems strange this would happen since GL is a state machine.

### #11Sik_the_hedgehog  Members

2951
Like
0Likes
Like

Posted 10 July 2013 - 11:25 PM

Using blending will also disable this hardware optimization, for PowerVR at least.  They call this feature "Tile Based Deferred Rendering" in case anyone wants to look it up.  Those little machines can handle a lot until you turn on blending, and presumably pixel shader depth writes.  Once you do this they slow to a crawl.

That would explain how they got OpenGL ES to work on that hardware in the first place.

I know what the algorithm does, it was used in the Dreamcast too, it's basically like an extremely simplified version of raytracing more or less. The more obvious issue, as you can imagine, is that such a thing doesn't even need the depth buffer at all, it processes all triangles and sorts them together (which is also how the Dreamcast got sort-independent alpha blending). I imagine the main reason Sega went with it back then is that it allowed for many more triangles at a much smaller fillrate.

I don't know how different is the current TBDR compared to the one from back then, but I presume that to get the same performance gains it must cheat a lot to work with OpenGL ES.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #12samoth  Members

8954
Like
0Likes
Like

Posted 11 July 2013 - 01:42 AM

Well as this article (which I didn't initially read) suggests, some cards support this and they use the word "explicitly" which implies that there is an "implicit" case.
ATI and NVIDIA are going to be supporting this early optimization. I don't know anything that suggests otherwise and have read some internal docs.

This extension also provides the capability to explicitly enable "early"
per-fragment tests, where operations like depth and stencil testing are
performed prior to fragment shader execution.  In unextended OpenGL,
fragment shaders never have any side effects and implementations can
sometimes perform per-fragment tests and discard some fragments prior to
executing the fragment shader.

I think this extension is worded in a way that may be somewhat misleading, though at least they've put "early" in quotes. The same goes for the (wiki, by the way, so caveat emptor) page on opengl.org that you've dug up.

There is no "forcing early z" in OpenGL. OpenGL does not have any such thing as an "early z" at all, so you cannot enforce it. The specification is very clear about when the z test happens, and it is not "early", it is after the fragment shader has run. Still, implementations are allowed to do something different as long as the observable result is exactly identical, and most modern implementations in fact do something different.

If you search the OpenGL specification for "early", you find 3 occurrences of "linearly" and two occurrences of "clearly" (because Adobe Reader has no notion of searching for whole words), but "early" has no appearance at all. In particular, the additions to chapter 3 in above extension spec are funny because for example section 3.12.2 does not even exist in my copy of the specification (it stops at 3.11). They must be using a different copy

A better wording would be that you can give a strong hint to the implementation which effectively forces early z test on implementations that do an early z test (or, on most mainstream implementations).

The thing is, a modern implementation would of course always like to do the z test early, because this saves shader work. Insofar there is no need to "force" it. It's trying hard to do it anyway. However, the implementation must still guarantee that the result is the same, which it can only do with some very harsh constraints (for example if the shader does not modify z, so it is already known what the value will be long before the shader runs).

Now, by using a qualifier that tells the implementation so-and-so, you give a promise (for example "depth will not change" for "depth will always be greater"), or in the second example that you've given, you ask for a specific behaviour.

By doing so, you give a promise to the implementation that you know what you're doing, and that you guarantee that whatever you do will not cause the results to be wrong if it performs the early z optimization. You can of course break your promise or do something that will not work with the behaviour that you request, but this is very unwise -- that'd be welcome to the land of undefined behaviour.

Taking your word on that promise, the implementation will of course do the optimization (that's pretty much guaranteeed). In a way, you could maybe interprete this as "force on", but it really isn't. It's more "enabling" or "allowing" the implementation to do something outside the specification.

Edited by samoth, 11 July 2013 - 01:52 AM.

### #13marcClintDion  Members

435
Like
0Likes
Like

Posted 11 July 2013 - 04:25 AM

This feature exists to ensure proper behavior when using Image Load Store or other incoherent memory writing.

To me, the part where it says, 'when using' seems fairly explicit.

"...this ensures that image load/store operations will only happen on fragments that pass the depth test."

I would say from what is written in that document that GL_ARB_conservative_depth is a filter for  GL_ARB_shader_image_load_store.

It states that GL_ARB_conservative_depth is meant to prevent operations on  GL_ARB_shader_image_load_store from being executed when the fragment fails the depth test.

I don't see anything that says they can be interchangeable replacements or substitutes for one another.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.

2052
Like
0Likes
Like

Posted 11 July 2013 - 02:17 PM

ic0de, I think the summary is openGL may or may not allow this, but the hardware for great graphics cards (AMD/NVIDIA ...Intel should as well), requires heavy optimization to make powerful cards. They will take care of it whether openGL does or not.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.