Bradley Odell
Bradley Odell

Reputation: 1258

OpenGL GLSL code is compiling when it shouldn't be


I know this sounds like an odd "problem" but stick with me.

I have a graphics card which supports OpenGL 3.3.
However, I am writing code to target OpenGL 2.0 and therefore GLSL version 1.1.
I have the following GLSL code:

#version 110

in vec4 position; // x,y,z,w (ignores w)

uniform mat4 mvpMatrix;

void main() {
    gl_Position = mvpMatrix * position;
}

As you can see, I have specified the GLSL version to be #version 110.
You might also notice that I use the 'in' keyword for the vec4 position.
This keyword is not available in the version of GLSL that's specified but it compiles without error on my graphics card (which again, supports OpenGL 3.3).
The correct keyword should be 'attribute'.
When tested on a computer that only has OpenGL 2.1 the shader code gets a compile error.

Why does my compiler not also compile with an error? Shouldn't it being compiling the shaders with version 110 compliance and report the appropriate error?
Is there anyway to check for this? Maybe it's a warning or something? I don't know. Help!

Also, I am programming in Java using LWJGL. If that helps any, but I don't think it has anything to do with the problem.

Edit:
I added code to always check the info log after a shader compile and it still doesn't report anything when using the 'in' keyword.

Upvotes: 2

Views: 140

Answers (2)

Reto Koradi
Reto Koradi

Reputation: 54592

It's always a good idea to check the compilation info log even if the compilation succeeded. It can contain warnings and other diagnostic information. In LWJGL, you can get the info log for a shader with id shaderId by calling:

int logLen = GL20.glGetShaderi(shaderId, GL20.GL_INFO_LOG_LENGTH);
GL20.glGetShaderInfoLog(shaderId, logLen);

Unfortunately it's quite common for GLSL compilers to leave errors unreported. My guess is that most testing happens with correct shaders, and error handling is not validated nearly as well, and prioritized as highly.

Also, once a vendor releases a version that (accidentally) lets certain errors pass, they might not want to add the stricter error checks later. The risk is that it would break already released software that only works because of the incomplete error checking.

Upvotes: 0

GuyRT
GuyRT

Reputation: 2917

Different compilers differ a lot as to what GLSL they will accept. It's worth looking at the compilation log even if your shader compiles successfully. The log can contain warnings as well as errors.

In LWJGL it looks like the way to check the log is:

private static String getLogInfo(int obj) {
  return ARBShaderObjects.glGetInfoLogARB(obj,
      ARBShaderObjects.glGetObjectParameteriARB(obj, 
      ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB));
}

(code is at line 129 of Box.java)

Upvotes: 1

Related Questions