Created
March 5, 2014 01:58
-
-
Save chrisledet/9359792 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static android.opengl.GLES20.GL_COMPILE_STATUS; | |
import static android.opengl.GLES20.GL_FRAGMENT_SHADER; | |
import static android.opengl.GLES20.GL_LINK_STATUS; | |
import static android.opengl.GLES20.GL_VALIDATE_STATUS; | |
import static android.opengl.GLES20.GL_VERTEX_SHADER; | |
import static android.opengl.GLES20.glAttachShader; | |
import static android.opengl.GLES20.glCompileShader; | |
import static android.opengl.GLES20.glCreateProgram; | |
import static android.opengl.GLES20.glCreateShader; | |
import static android.opengl.GLES20.glDeleteProgram; | |
import static android.opengl.GLES20.glDeleteShader; | |
import static android.opengl.GLES20.glGetProgramiv; | |
import static android.opengl.GLES20.glGetShaderiv; | |
import static android.opengl.GLES20.glLinkProgram; | |
import static android.opengl.GLES20.glShaderSource; | |
import static android.opengl.GLES20.glValidateProgram; | |
public class Shader { | |
public static int compileShader(int type, String code) { | |
final int shaderObjectId = glCreateShader(type); | |
if (shaderObjectId == 0) { | |
// Could not create a new shader | |
return 0; | |
} | |
// Pass shader source | |
glShaderSource(shaderObjectId, code); | |
// Compile code | |
glCompileShader(shaderObjectId); | |
// Check Compile status, store it in first element in compileStatus array | |
final int[] compileStatus = new int[1]; | |
glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0); | |
if (compileStatus[0] == 0) { | |
// If it failed, delete the shader object | |
glDeleteShader(shaderObjectId); | |
return 0; | |
} | |
return shaderObjectId; | |
} | |
public static boolean validateProgram(int projectObjectID) { | |
glValidateProgram(projectObjectID); | |
final int[] validateStatus = new int[1]; | |
glGetProgramiv(projectObjectID, GL_VALIDATE_STATUS, validateStatus, 0); | |
return validateStatus[0] != 0; | |
} | |
public static int linkProgram(int vertexShaderID, int fragmentShaderID) { | |
final int programObjectID = glCreateProgram(); | |
if (programObjectID == 0) { | |
// Could not create program for shader | |
return 0; | |
} | |
glAttachShader(programObjectID, vertexShaderID); | |
glAttachShader(programObjectID, fragmentShaderID); | |
glLinkProgram(programObjectID); | |
final int[] linkStatus = new int[1]; | |
glGetProgramiv(programObjectID, GL_LINK_STATUS, linkStatus, 0); | |
if (linkStatus[0] == 0) { | |
// Could not link to program | |
glDeleteProgram(programObjectID); | |
return 0; | |
} | |
return programObjectID; | |
} | |
public static int compileVertexShader(String code) { | |
return compileShader(GL_VERTEX_SHADER, code); | |
} | |
public static int compileFragmentShader(String code) { | |
return compileShader(GL_FRAGMENT_SHADER, code); | |
} | |
public static int buildProgram(String vertexShaderSource, String fragmentShaderSource) { | |
int program; | |
int vertexShader = compileVertexShader(vertexShaderSource); | |
int fragmentShader = compileFragmentShader(fragmentShaderSource); | |
program = linkProgram(vertexShader, fragmentShader); | |
if (!validateProgram(program)) { | |
return 0; | |
} | |
return program; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment