10

I'm trying to compile a shader program in OpenGL 3.2, but I'm getting a strange linking error.

After creating the vertex and fragment shaders compiling and attaching them, I try to link them into a program but I get the following infolog error:

ERROR: Compiled vertex shader was corrupt.
ERROR: Compiled fragment shader was corrupt.

I have absolutely no idea what it means and the only thing I could find on google was to ignore it. However, when I glUseProgram() it I get an invalid operation, so I can't just ignore this error.

Moreover, I just updated to XCode 5 and the very same code/shader source was working. Don't know how it can be related though..

Edit: shader source

Vertex:

#version 150

in vec3 position;

uniform mat4 worldMatrix;
uniform float time;

out vec3 outPos;
void main(){
    gl_Position = worldMatrix*vec4(position, 1.0);
    outPos = position;
}

Fragment:

#version 150

out vec4 outColor;
uniform float time;
uniform float red;
uniform float green;
uniform float blue;

void main(){
    outColor=vec4(red, green, blue,1.0);
}
4
  • Do you log compilation errors too, and can you show the shader code? Commented Sep 26, 2013 at 10:44
  • I totally forgot to put the shader code. It's in the edit. There are no compilation errors reported. Commented Sep 26, 2013 at 10:49
  • 1
    You have out outPos in vertex shader, but no corresponding in parameter in fragment shader. From the looks of shader code you could remove this variable completely - you don't use it anyway. Commented Sep 26, 2013 at 11:28
  • Is your string properly null-terminated? There could be non-printing characters at the end of both of these strings. As for what this error message means, who knows? That is a problem with GLSL, every vendor implements their own compiler and there are no standardized error messages :-\ It would help if we knew your driver vendor though. Commented Sep 26, 2013 at 13:19

2 Answers 2

7

Got it to work.

At first I rewrote the shaders with another editor (text mate) and then it worked sometimes. Then I made sure that it was properly null terminated and it worked every time.

Maybe somehow there were non-printing characters like Andon M. Coleman suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

What does properly null terminated mean here? I keep seeing this as the issue causing this but I don't know how to check it mine are.
7

I had the same issue, and discovered that if you use the ' std::stringstream buffer' to read the file, as many code examples on the web use, the method .str().c_str() to get a *ptr needed for glShaderSource, the pointer gets deleted, meaning, you get random linker errors. Here is the work around I created...

int shaderFromFile(const std::string& filePath, GLenum shaderType) {  
  //open file
  std::ifstream f;
  f.open(filePath.c_str(), std::ios::in);
  if(!f.is_open()){
    throw std::runtime_error(std::string("Failed to open file: ") + filePath);
  }

  //read whole file into stringstream buffer
  std::stringstream buffer;
  buffer << f.rdbuf();
  buffer << "\0";
  f.close();
  // need to copy, as pointer is deleted when call is finished
  std::string shaderCode = buffer.str().c_str(); 

  //create new shader
  int ShaderID = glCreateShader(shaderType);

  //set the source code

  const GLchar* code = (const GLchar *) shaderCode.c_str();

  glShaderSource(ShaderID, 1, &code, NULL);
  //compile
  glCompileShader(ShaderID);

  //throw exception if compile error occurred
  GLint status;
  glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &status);
  std::cout << "Status from compile:" << status << "\r\n";
  if (status == GL_FALSE) {
      std::string msg("Compile failure in shader:\n");

      GLint infoLogLength;
      glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
      char* strInfoLog = new char[infoLogLength + 1];
      glGetShaderInfoLog(ShaderID, infoLogLength, NULL, strInfoLog);
      msg += strInfoLog;
      delete[] strInfoLog;

      glDeleteShader(ShaderID); ShaderID = 0;
      throw std::runtime_error(msg);
  }

  return ShaderID;
}

1 Comment

Thank you very much for figuring out this weird bug. Would have kept me up all night otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.