I try to link a basic shader program with a vertex and fragment shader. Both shaders compile successfully, but if I try to link the program the linking always fails. Whats wrong, why the linking is failing? The info log is empty, but I get GL_INVALID_OPERATION error, when linking. I run the MAIN code in the "onSurfaceCreated" method from the GLSurfaceView.Renderer interface.
MAIN:
String vertexShaderSource = "attribute vec4 a_Position; " +
"void main() " +
"{ " +
" gl_Position = a_Position; " +
"} ";
String fragmentShaderSource = "precision mediump float; " +
"void main() " +
"{ " +
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);" +
"} ";
int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShaderHandle, vertexShaderSource);
ShaderHelper.compileShader(vertexShaderHandle);
int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShaderHandle, fragmentShaderSource);
ShaderHelper.compileShader(fragmentShaderHandle);
programHandle = GLES20.glCreateProgram();
GLES20.glAttachShader(programHandle, vertexShaderHandle);
GLES20.glAttachShader(programHandle, fragmentShaderHandle);
ProgramHelper.linkProgram(fragmentShaderHandle);
HELPER:
public static void compileShader(int shaderHandle)
{
GLES20.glCompileShader(shaderHandle);
int[] results = new int[1];
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, results, 0);
if(results[0] != GLES20.GL_TRUE)
{
String infoLog = GLES20.glGetShaderInfoLog(shaderHandle);
throw new RuntimeException("Failed to compile shader!" + '\n' + infoLog);
}
}
public static void linkProgram(int programHandle)
{
GLES20.glLinkProgram(programHandle);
int[] results = new int[1];
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, results, 0);
if(results[0] != GLES20.GL_TRUE)
{
String infoLog = GLES20.glGetProgramInfoLog(programHandle);
throw new RuntimeException("Failed to link program!" + '\n' + infoLog);
}
}
EDIT:
WTF, the program links fine, if I don't use the helper method to link. Why???
Your code passes the wrong argument to the linkProgram()
method:
programHandle = GLES20.glCreateProgram();
...
ProgramHelper.linkProgram(fragmentShaderHandle);
As the name suggests, fragmentShaderHandle
is the fragment shader object. For linking the program, you need to pass the program object:
ProgramHelper.linkProgram(programHandle);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.