2.3.4. Shader programming

When using OpenGL ES 2.0, you must always provide a vertex shader and a fragment shader program. The vertex shader program is executed once for each vertex that is passed to the API calls. The fragment shader program is executed once for each resulting fragment.


Fixed-function hardware such as the Mali-55 GPU does not support shader programs.

The simple example application in Figure 2.2 illustrates communication between the application and shader programs.

Figure 2.2. Graphics application and shader program communication

The communication flow shown in Figure 2.2 is:

  1. To specify vertex coordinates, your application calls glBindAttribLocation(). The coord attribute specifies the vertex coordinates x, y, and z.


    To have any effect, the application must call glBindAttribLocation() before the program is linked.

    The application then enables the array using glEnableVertexAttribArray.

    The function glVertexAttribPointer() provides values for the vertex coordinates. In the example, the value 3 indicates that there are three components for each vertex x, y, and z. The array is the array of vertex coordinates. The vertex shader is run once for each vertex. Each time the program runs, it starts with its set of such vertex attributes, as values of attribute variables.

  2. The application can similarly supply uniform values to the shader program. When calling one of the glUniform functions, your application must already have called glGetUniformLocation to find the location value to use. The call to the glUniform function specifies the value that appears in that uniform variable in the shader programs. The uniform variable value remains the same for all elements drawn, until your application makes another call to glUniform, with the same location.

  3. The homogenous coordinates of the vertex position are output from the vertex shader program by assigning the values to the standard, built-in variable gl_Position.

  4. Other values are output from the vertex shader by assigning them to varying variables. For example nrm in the shader program in Figure 2-2. These are special variables that store interpolated values. Varying values from the vertex shader program become the values of the varying variables in the fragment shader program. More precisely, the varying variables for the three corner vertices are interpolated to produce values for all the fragments of each triangle.

  5. Uniform variables specified by the application appear with values in the fragment shader in the same way as in the vertex shader.

  6. The fragment color result from the fragment shader is generated by assigning an RGBA value to the standard variable gl_FragColor.

Copyright © 2007-2009 ARM. All rights reserved.ARM DUI 0363D