Compiling Android Shaders

This process closely parallels the WebGL compilation process. Even the method names are the same, howeve except calling then on a gl context object, the methods are static methods on the GLES20 class.

 68     protected int compileShader(String shaderSource, int shaderType)
 69     {
 70         IntBuffer logStatus;
 71         int       shader;
 72         String    compileLog;
 73         int[]     compileStatus;
 74 
 75         shader = GLES20.glCreateShader(shaderType);
 76         // Pass in the shader source.
 77         GLES20.glShaderSource(shader, shaderSource);
 78         // Compile the shader.
 79         GLES20.glCompileShader(shader);
 80 
 81         // Get the compilation status.
 82         compileStatus = new int[1];
 83         GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
 84 
 85         // Error compile status, get the relevant log.
 86         if (compileStatus[0] == 0)
 87         {
 88             logStatus     = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
 89             GLES20.glGetShaderiv(shader, GLES20.GL_INFO_LOG_LENGTH, logStatus);
 90             if (logStatus.get(0) > 0)
 91             {
 92                 compileLog = GLES20.glGetShaderInfoLog(shader);
 93                 Log.d(appName, "Shader compilation failed with, " + compileLog);
 94             }
 95             else
 96             {
 97                 // Workaround for issue 9953, where GL_INFO_LOG_LENGTH is zero.
 98                 Log.d(appName, "Shader compilation failed for an unknown reason.");
 99             }
100             GLES20.glDeleteShader(shader);
101             shader = 0;
102         }
103         
104         return shader;
105     }

Notes

This is the same process as used in WebGL. Just the naming is a bit diffrent.

For example, in WebGL we invoke gl.createShader to create a shader, and in OpenGL ES we invoke GLES20.glCreateShader.

The overall process remains the same.