Why does glValidateProgram fail when no VAO is bou

2019-05-14 15:50发布

问题:

I have a problem validating my shader program in LWJGL/OpenGL 3.
I read the documentation, but I can't seem to find a reason why a VAO is needed when calling glValidateProgram.

int program = glCreateProgram();
int vertexShader = glCreateShader(...);
int fragmentShader = glCreateShader(...);
// ... vertex and fragment shader loading, compiling, errorchecking ...
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "color");
glLinkProgram(program);
glDetachShader(program, shader);
glDetachShader(program, shader);
glValidateProgram(program);
if (glGetProgrami(program, GL_VALIDATE_STATUS) != GL_TRUE)
    System.exit(-1);

This exits the program without any error message.
GL_LINK_STATUS is OK and GL.getErrors() also has nothing to report.
But when creating a VAO around glValidateProgram it works just fine.
I can also just ignore the fact that glGetProgrami returns GL_FALSE and just run the shader program.

What i mean with creating a VAO around glValidateProgram():

int vao = glGenVertexArrays();
glBindVertexArray(vao);
glValidateProgram(program);
if (glGetProgrami(program, GL_VALIDATE_STATUS) != GL_TRUE)
    System.exit(-1);
glDeleteVertexArrays(vao);

When I do this, GL_VALIDATE_STATUS is true and I can draw my stuff.

The used shaders are simple passthrough shaders.
The vertex shader returns the position and the fragment shader returns the color.

So, why do I have to bind an VAO even though I can immediately delete it after the validation?

回答1:

This behavior matches the OpenGL spec. I'm using the OpenGL 3.3 spec as reference.

In appendix E.2.2 "Removed Features" on page 344, it says:

The default vertex array object (the name zero) is also deprecated. Calling VertexAttribPointer when no buffer object or no vertex array object is bound will generate an INVALID_OPERATION error, as will calling any array drawing command when no vertex array object is bound.

The spec for glValidateProgram() on page 82 says:

ValidateProgram will check for all the conditions that could lead to an INVALID_OPERATION error when rendering commands are issued, and may check for other conditions as well.

So because issuing a draw command without a VAO bound will given a GL_INVALID_OPERATION error, and glValidateProgram() checks if a draw command would give a GL_INVALID_OPERATION error, what you're seeing is exactly as expected.