Sign in to follow this  
ic0de

OpenGL Forcing early Z, which extension to use?

Recommended Posts

ic0de    1012

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;

GL_ARB_shader_image_load_store:

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? 

Share this post


Link to post
Share on other sites
ic0de    1012

http://www.opengl.org/wiki/Image_Load_Store

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.

Share this post


Link to post
Share on other sites
dpadam450    2357

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.

Share this post


Link to post
Share on other sites
ic0de    1012

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:

http://www.opengl.org/registry/specs/ARB/shader_image_load_store.txt

 

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

Share this post


Link to post
Share on other sites


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).

Share this post


Link to post
Share on other sites
marcClintDion    435
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. 

Share this post


Link to post
Share on other sites
dpadam450    2357
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.

Share this post


Link to post
Share on other sites
dpadam450    2357

If you investigate this page it further supports my claim:
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........

Share this post


Link to post
Share on other sites
dpadam450    2357

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites
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 smile.png

 

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

Share this post


Link to post
Share on other sites
marcClintDion    435
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.

Share this post


Link to post
Share on other sites
dpadam450    2357

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now