Implementing glAlphaFunc in OpenGL ES2.0

In OpenGL ES 2.0 glAlphaFunc isn’t available, you have to implement it in a fragment shader. There isn’t a lot of reference out there for this (not that I could find anyway) so I thought I’d write this up.

It’s actually quite simple to implement a quick alpha test. I needed to be able to cookie cut out sprites and fonts so I simply needed to reject fragments where the alpha value was zero. Here are the guts of a shader to do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#ifdef GL_ES 
precision highp float;
#endif 
uniform sampler2D u_tex0; 
uniform bool u_alphatestenable; 
varying vec2 v_texCoord;
varying vec4 v_color;
void main(){ 
    //calculate the fragment color based on the texture and the vertex colour
    vec4 basecolor = texture2D( u_tex0, v_texCoord ) * v_color;

    //if testing is enabled, check the alpha component and discard if zero      
    if(u_alphatestenable){ 
        if(basecolor.a == 0.0){ 
            //throw this fragment away
            discard;
        }
    }
    gl_FragColor = basecolor;
}

You need to set up a uniform variable u_alphatestenable which enables the alpha test. If you want to support different test types ( less than, greater than etc) then you will need two more uniform variables: one for the test type and one for the value to test against.

To enable the test you need to set uni_alphatest_enable to true:

1
2
3
int uni_alphatest_enable = glGetUniformLocation(mProgram, "u_alphatestenable"); 
bool alphateston = true; 
glUniform1i(uni_alphatest_enable, alphateston)

note that you shouldn’t call glGetUniformLocation every frame, it should be cached somewhere. It’s quite simple and while you may be thinking oh that is so slow it not that bad. It’s faster then the fixed function pipeline which is doing tests for alpha, lights, blend modes etc. If you get paranoid then you can create multiple shaders that support different subsets of features. All you need to be careful of is the cost of calling glSetProgram (to switch shaders) which can be expensive and cause bubbles in the vertex pipeline in the hardware.

Hope it helps.

dazza