Setting the rendering order for objects to avoid overdraw is useful, but it is not always possible to specify the rendering order for each object.
For example, if you have a set of objects with computationally expensive
shaders and the camera can rotate around them freely, some objects that were at the back
can move to the front. In this case, if there is a static rendering order set for these
objects, some might be drawn last, even if they are occluded. This can also happen if an
object can cover parts of itself.
In these cases, you can use a depth-prepass to reduce overdraw. The
depth-prepass renders the geometry without writing colors in the framebuffer. This
initializes the depth buffer for each pixel with the depth of the nearest visible
object. After this pre-pass the geometry is rendered as usual but using the Early-Z
technique, only the objects that contribute to the final scene are actually rendered.
Extra vertex shader computations are required for this technique because the vertex
shader is computed two times for each object, once for filling the depth buffer and
another for the actual rendering. This technique is very useful if your game is fragment
bound and you have spare capacity in the vertex shader.
In Unity, to do a rendering pre-pass for objects with custom shaders, add an
extra pass to your shaders:
// extra pass that renders to depth buffer only
After adding this pass, the frame debugger shows that the objects are
rendered twice. The first time they are rendered there are no changes in the color
You can see the depth buffer by choosing it in the top left menu of
the frame debugger.