Zebrafish
Zebrafish

Reputation: 13936

Is depth-sorting redundant if using depth-prepass?

To avoid overdraw (shading the same pixel twice) it's beneficial to draw objects front to back. When you draw an object in the front the depth values are written to the depth buffer, then when you draw an object behind it the pixel can be rejected if it's depth value doesn't pass the comparison operation. However I've been wondering whether there's any benefit to depth sorting if you do a complete depth pre-pass. The only way I can see that there's a benefit to sorting front to back is if the testing of the depth is separate from the writing. Because the pixel depth value is read in order to compare it with the currently rasterised triangle the number then the number of depth buffer reads is ALWAYS the same, no matter whether rendering front to back or back to front.

However the number of writes to the depth buffer (to replace the value with a new lowest/highest value) does seem dependent on whether you sort front to back or back to front. I don't know much about it but I'm led to believe that the depth test and write are the same one operation (disabling depth tests also disables depth writes). If that's the case then sorting won't matter.

So does it make a difference? And if so or if not, why?

Also, consider whether there's a difference if the depth pass for an object has a fragment shader attached which writes to depth (which is necessary for alpha tested writes, such as chain link fences, trees, etc.) My intuition is that alpha tested objects won't make a difference, since if you write to the depth buffer, early depth test cannot be done, and therefore the fragment shader needs to run regardless. And so in this case the same question applies, except the depth is read and written at the end of the fragment shader execution, the question still being whether only testing (and not writing), is faster than both testing and writing (ie., is there benefit to depth sorting for depth prepass?).

Upvotes: 0

Views: 1108

Answers (2)

Spektre
Spektre

Reputation: 51845

there are gfx techniques that requires depth sorting regardless or in addition to depth buffering ...

For example dealing with transparency because rendering transparent polygons in wrong order cause wrong rendering result.

I expect there are more techniques and effects similar to this I imagine volumetric lighting, clouds, light gaps, and similar ...

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 473567

Sorting with the depth pre-pass can still be helpful. Some depth buffers have functionality that can cull entire groups of fragments with a single test. Hierarchical depth buffers, Hi-Z, whatever the particular name for it is, these technologies exist. And they work best when you render in a roughly front-to-back order.

Upvotes: 2

Related Questions