Dreaded shader link error with no message!

Hi All,

Starting to loose the plot a little bit. Cannot for the life of me get my shaders to compile. No errors from the shaders but program will not link and does not give me any errors… code below;


#include <shader.h>

//Constructor
Shader::Shader(const char *vurl, const char *furl) {

	// ---------------------------------------
	// create a shader log for output
	// ---------------------------------------
	Logs shaderLog("shaderlog.txt");

	// ---------------------------------------
	// lets load the source of the vertex shader
	// ---------------------------------------
	char *vSrc;
	char *fSrc;
	int vLen;
	int fLen;
	std::fstream vfile;
	vfile.open(vurl, std::ios::in);

	//get the length of the file and store it
	vfile.seekg (0, vfile.end);
    vLen = vfile.tellg();
    vfile.seekg (0, vfile.beg);

	//allocate memory to store the data
    vSrc = new char[vLen];
	//read into the memory
    vfile.read(vSrc, vLen);

	//now close the file
	vfile.close();

	// ---------------------------------------
	// lets load the source of the fragment shader
	// ---------------------------------------
	std::fstream ffile;
	ffile.open(vurl, std::ios::in);

	//get the length of the file and store it
	ffile.seekg (0, ffile.end);
    fLen = ffile.tellg();
    ffile.seekg (0, ffile.beg);

	//allocate memory to store the data
    fSrc = new char[fLen];

	//read into the memory
    ffile.read(fSrc, fLen);

	//now close the file
	ffile.close();

	// ---------------------------------------
	// lets create our shaders
	// ---------------------------------------

	//create the shader objects
	GLuint vertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);

	//attach our shader source code
	glShaderSource(vertexShaderObject, 1, (const GLchar** )&vSrc, (const GLint*)&vLen);
	glShaderSource(fragmentShaderObject, 1, (const GLchar** )&fSrc, (const GLint*)&fLen);

	//lets compile them
	glCompileShader(vertexShaderObject);
	glCompileShader(fragmentShaderObject);

	//check for compile errorsGLint compiled;
	GLint vsuccess = 0;
	GLint fsuccess = 0;

	glGetShaderiv(vertexShaderObject, GL_COMPILE_STATUS, &vsuccess);
	if(vsuccess == GL_FALSE) {
		shaderLog.write("Vertex Shader Compilation Failed!");
		//get info from compile for debugging
		GLint vSize = 0;
		glGetShaderiv(vertexShaderObject, GL_INFO_LOG_LENGTH, &vSize);
		GLchar *vlog = new GLchar[vSize];
		glGetShaderInfoLog(vertexShaderObject, vSize, NULL, vlog);
		shaderLog.write((const char *)vlog);
	}

	glGetShaderiv(fragmentShaderObject, GL_COMPILE_STATUS, &fsuccess); 
	if(fsuccess == GL_FALSE) {
		shaderLog.write("Fragment Shader Compilation Failed!");
		GLint fSize = 0;
		glGetShaderiv(fragmentShaderObject, GL_INFO_LOG_LENGTH, &fSize);
		GLchar *flog = new GLchar[fSize];
		glGetShaderInfoLog(fragmentShaderObject, fSize, NULL, flog);
		shaderLog.write((const char *)flog);
	}

	//now lets link into the shader program
	this->program = glCreateProgram();
	glAttachShader(this->program, vertexShaderObject);
	glAttachShader(this->program, fragmentShaderObject);
	glLinkProgram(this->program);

	//now lets check that the program created?
	GLint linked;
	glGetProgramiv(this->program, GL_LINK_STATUS, &linked);
	if (linked != GL_TRUE)	{
		shaderLog.write("Shader program failed to link!");
		switch (linked) {
			case GL_INVALID_VALUE:
				shaderLog.write("GL_INVALID_VALUE");
				break;
			case GL_INVALID_OPERATION:
				shaderLog.write("GL_INVALID_OPERATION");
				break;
			case GL_INVALID_ENUM:
				shaderLog.write("GL_INVALID_ENUM");
				break;
			default:
				shaderLog.write("Unknown Error!");
		}

		//get info from compile for debugging
		GLint pSize = 0;
		glGetProgramiv(this->program, GL_INFO_LOG_LENGTH, &pSize);

		if(pSize > 0) {
			//create the log and clear it
			GLchar *plog = new GLchar[pSize];
			for(int i = 0; i < pSize; i++) {
				plog[i] = 0;
			}
			glGetShaderInfoLog(this->program, pSize, NULL, plog);
			shaderLog.write((const char *)plog);

		}

		glDeleteProgram(this->program);
	}

	// ---------------------------------------
	// Shader Creation is completed!
	// ---------------------------------------

	//Close the shader log
	shaderLog.close();
	glDetachShader(this->program, vertexShaderObject);
	glDetachShader(this->program, fragmentShaderObject);
	glDeleteShader(vertexShaderObject);
	glDeleteShader(fragmentShaderObject);
}

Vertex Shader


#version 330 core

in vec4 position;

void main(void) {
	gl_Position = position;
}

Fragment Shader


#version 330 core

void main(void) {
	gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}

Output


Shader program failed to link!
Unknown Error!

Any help would be appreciative. I have used SDL2 to create the window and context then GLEW to setup the extensions.

The value for GL_LINK_STATUS is either GL_TRUE or GL_FALSE, not a glGetError() enumerant.

You should be calling glGetProgramInfoLog() here, not glGetShaderInfoLog().

The shader compilation and program linker status is not an OpenGL error; it is a boolean value, so I don’t really know what the point of that switch statement is. Also, never test a boolean value against GL_TRUE; you should test it against GL_FALSE (or just if(!linked)).

Thanks for spotting the error;

You should be calling glGetProgramInfoLog() here, not glGetShaderInfoLog().

this was the issue for me not seeing the errors! Now i am back in action!

Thanks