Early z-test and GL_STENCIL_TEST

Started by
4 comments, last by _the_phantom_ 19 years, 6 months ago
I'm working on a GFX5200, and in my engine I use the early-z lay, to take advantage of early z-test. However, it seems that if stencil test is enabled, this early-z test is not applied. Actually, I was hoping that my card could do early stencil tests too, but not only it doesn't do that, it disables early-z too. Is that true? Why this happens? Is there any way I can use stencil test and early z at the same time?
Advertisement
The reason it happens is all down to the OpenGL spec.
Gfx cards can only perform early z-rejection if it doesnt impact the way the scene is drawn, which means that it shouldnt really effect any tests which come before it, the stencil test being one of these. Therefore, if the stencil test is active the gfx card has to turn off early z-rejection to enable the pipeline to work correctly.

Remember, early z-rejection accures before the fragment is processed, however the stencil test adjusts what happens to the fragment is performed AFTER the fragment is processed (either FF or shader) and can prevent the depth test from being used at all and adjust the scene.

So, in short, no, you cant use early z-rejection with the stencil test, it doesnt make any sense to allow it with regards to the spec.
Ok, so stencil does not work with early z.

But I disagree that the reason can be found in the GL specs, because they don't even mention early z.According to the specs, all tests happen after fragment processing(either fixed or programmed),and stencil tests happen before depth tests.Early z-culling is just an implementation optimization.

I can't think of a reason why early z-culling could not work with stencil tests.If the fragment is to be z-culled, it will be rejected anyway. The way you mean it, early z is disabled because the fragment must go through the stencil test no matter what,if GL_STENCIL_TEST is enabled.But what is the point of doing a stencil test if we already know that it will be rejected anyway at a later point?

-EDIT: Ok, now that I'm thinking about it, if we had stencil tests and early z,that would mean the depth test would take place before the stencil test. I don't think it would produce incorrect results, but it would be against the specs. On the other hand, the specs state that tests happen after fragment processing, so one can say that early z-culling is against the specs anyway.
indeed, early z is against the specs, however as it doesnt impact later tests if they are non-enabled it isnt an issue.

I guess the logic is along the lines of if you've enabled the tests you could need the result, for some reason so they have to perform them, however if they are off then there is no reason to not perform early z as you've no dependancy on the outcome of later tests.
Quote:Original post by _the_phantom_
I guess the logic is along the lines of if you've enabled the tests you could need the result, for some reason so they have to perform them...


Wow, I don't really think straight today. Of course we need the result of the stencil test, because the stencil buffer is updated(glStencilOp) even if depth-test or stencil-test fails. If we culled the fragment with early z-test before the stenciling, I don't think Carmack's reverse for example would be possible.

I guess the only solution would be to have early stencil test too, before the early z-test. I think I've heard that something like this, but it seems my card doesn't support it.
cant say i've heard of it, and it would be pretty damn hard todo as the stencil op relies on the output of any per-fragment operations which come before it so that it has input, which means the fragment has to be processed to generate that data to feed into the test

This topic is closed to new replies.

Advertisement