As for depth testing, DirectX says it should appear that depth testing happens after the pixel shader, it does not say that it is actually necessary. In fact, early z has existed for many years on equipment from many manufacturers. Often there is even an early form of Z-testing called Hierarchical Z, which doesnโt work on individual pixels, but on โtilesโ of many pixels at a time to avoid the cost of early Z.
Early Z is not something you can turn on or off in any particular state that you have installed on the device. The hardware will perform z-testing as early as possible and in such a way that you do not know that it is somewhere else than after the pixel shader.
There are certain things you can do, although this may limit the hardware in its ability to perform z-testing, as it might otherwise. Alpha testing, the use of "drop" (killing a pixel) and alpha coverage will certainly disable the early z recordings, since pixel shaders must be run before the equipment can determine whether to write the depth value or not. If you use alpha testing / discarding and don't need z-write, turn them off and you will have the best opportunity for early z to be available.
Modifying / writing "depth" in a pixel shader is definitely a no-no if you want an early Z. In this situation, the hardware cannot even perform an early test, because it does not yet recognize the pixel depth until you solve it in the pixel shader, it cannot run an early z-test or an early z-record.
If you need to write depth from a pixel shader, but you can guarantee that you will only write a depth value greater than or equal to that created by the rasterizer, you can use semantics of semantics without the undocumented SV_DepthGreater . Since you promise not to write the depth value less than the interpolated depth, the hardware can still try and run the early z test, but then defer the z record to the end of the pixel shader. (There is the equivalent of SV_DepthLessEqual if you use inverted z-test / z-buffer).
Since it should appear that z-testing occurs at the end of the pipeline, the use of UAVs in the pixel shader will also disable the early z. Since the goal of rendering is now not the only way out, and since the DirectX specification says that it should appear that z-testing happens at the end, it follows that your entries in the UAV should occur even for pixels that eventually come out of for the z-test. For this reason, an attribute was added to Shader Model 5 called [earlydepthstencil] , which tells DirectX that you are happy at the beginning of z (if possible), and not start the pixel shader, even if you have UAV records.
So, if you are not doing some of the slightly bizarre things mentioned above (changing depth using SV_DEPTH, alpha coverage, using clip / reset, etc.), chances are you already get the advantage of early Z. Always turn off z- records if you do not want them, and avoid writing to SV_DEPTH, since you absolutely do not get early z with it turned on.