Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Compiled Shader was corrupt

Tags:

opengl

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);
}
like image 550
zync Avatar asked Sep 26 '13 10:09

zync


2 Answers

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.

like image 67
zync Avatar answered Oct 31 '22 18:10

zync


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;
}
like image 5
Lido666 Avatar answered Oct 31 '22 20:10

Lido666