Avoid Using Alpha Test/Discard

The OpenGL ES 2.0 fragment shader operation ‘discard’ can be used to stop fragment processing and prevent any buffer updates for the current fragment in a shader. Essentially, it provides the same functionality as the fixed function ‘alpha test’ but in a programmable manner. It can seem like a convenient method to achieve the rendering of complex shapes without using geometry, but it is an expensive operation on all modern graphics hardware and thus is discouraged.

On modern graphics hardware, the use of ‘alpha test’ requires that the fragment shader be run for a given fragment before visibility can accurately be determined. This affects performance on PowerVR as the visibility information must be fed back from the fragment processing stage to the ISP before the ISP can continue to perform depth and stencil tests for other polygons in that position. This effectively removes some of the benefits of PowerVR’s Hidden Surface Removal (and those of ‘Early-Z’ techniques on other architectures).

For this reason ‘discard’ and ‘alpha test’ should be avoided whenever possible.

It should also be noted that if a shader contains the ‘discard’ keyword then any object that shader is applied to will suffer the cost of alpha test, even if the keyword is inside a conditional block that the developer knows will not be hit for a draw call. The GPU cannot know the result of the conditional without executing the fragment shader and so has to assume that the ‘discard’ keyword may be hit. The solution to this is to move the use of ‘discard’ into a separate shader.

Under fixed function APIs it is essential that ‘alpha test’ be disabled for objects that do not require it. It is common for ‘alpha test’ to be switched on at the beginning of a scene and left on for the entire scene. This is strongly discouraged is it may severely harm performance.

The same visual effect as ‘discard’ can often be achieved through the use of the correct Alpha Blend Mode, and setting the Alpha value to ‘0’ where ‘discard’ would be used.

If ‘discard’ or ‘alpha test’ cannot be avoided then objects using these techniques should be submitted after all opaque geometry is submitted.

Full document can be found here.