Issue: Linker failure: ERROR: Compiled vertex shader was corrupt.

Hello,

I writing as a beginner at using openGL, I need help with a issue concerning *.glsl files, these files work fine in Visual Studio, however when I use these files in Xcode I get the following error:

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

the triangles draw, but the colour will not, im not sure of what is wrong.

here are the file contents:

VertexShader.glsl

#version 400

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 colour;

smooth out vec4 vertColour;

void main()
{
vertColour = colour;
gl_Position = position;

}

TextureFragmentShader.glsl

#version 400

smooth in vec4 vertColour;
in vec2 UV;

out vec4 outputColour;

uniform sampler2D myTextureSampler;

void main()
{
outputColour = texture(myTextureSampler, UV).rgba * vertColour;
//outputColour = vertColour;
}

my system information:

OS X Mavricks 10.9.2

Xcode Version: 5.1

GLFW Version 4.0
Glew Version 1.10
files:

-libGLEW.a
-libglfw3.a
libGLEWmx.1.10.0.dylib
libGLEW.1.10.0.dylib
libglfw.3.0.gylib

IOKit.Framwork
Cocoa.Framework
OpenGl.Framework

What I have tried:

changed the #Include 400 to 350 to 200 - just thought i would try.

created new glsl files using text editor, text wrangler, and even imported files from a working Visual Studio version.

any Help will be much appreciated.

Kind Regards
Theodore.

Hi, it should be a problem of text encoding…
Open our glsl files with text edit or sublime text, take convert to text only and save as new file…
Will works :slight_smile:

That is from the info log you get when compiling the shader? Are the line endings CRLF? Does changing to LF make a difference?

Your vertex shader does not export “vec2 UV”.

Debug tips:

  1. OpenGL does not know anything about “files”. When you compile a shader, you pass a pointer to the shader text. Your file loading code is responsible for producing that pointer.
  2. Learn how to debug in your new IDE.
  3. Set a breakpoint on glShaderSource (or much less likely, glCreateShaderProgramv or glCompileShaderIncludeARB) and then examine the string pointer(s) you are passing to OpenGL. Is it NULL? Then debug your file loader.

Hello,

Thank you for your replies, here is the code I have in main, NOTE: it does not matter what I do to the glsl files it just says “Linker failure: Error:…” maybe its what is in my code that is the problem, and I’m only a beginner at programming and OpenGL, I am not sure what part of the code should be set to null although I do see that there are pointers that have been set to null in the “Gluint CreateProgram” Function as well as the “GLuint CreateShader” function.

I apologise if I am not very specific as I mentioned I am still learning.


#include "GLEW/GL/glew.h"
#include "GLEW/GL/glxew.h"
#include "glfw3.h"

#include <vector>
#include <string>
#include <fstream>

GLuint CreateShader(GLenum a_eShaderType, const char *a_strShaderFile)
{
	std::string strShaderCode;
	//open shader file
	std::ifstream shaderStream(a_strShaderFile);
	//if that worked ok, load file line by line
	if(shaderStream.is_open())
	{
		std::string Line = "";
		while(std::getline(shaderStream, Line))
		{
			strShaderCode += "
" + Line;
		}
		shaderStream.close();
	}
    
	//convert to cstring
	char const *szShaderSourcePointer = strShaderCode.c_str();
	
	//create shader ID
	GLuint uiShader = glCreateShader(a_eShaderType);
	//load source code
	glShaderSource(uiShader, 1, &szShaderSourcePointer, NULL);
    
	//compile shader
	glCompileShader(uiShader);
    
	//check for compilation errors and output them
	GLint iStatus;
	glGetShaderiv(uiShader, GL_COMPILE_STATUS, &iStatus);
	if (iStatus == GL_FALSE)
	{
		GLint infoLogLength;
		glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
        
		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
		glGetShaderInfoLog(uiShader, infoLogLength, NULL, strInfoLog);
        
		const char *strShaderType = NULL;
		switch(a_eShaderType)
		{
            case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
            case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
		}
        
		fprintf(stderr, "Compile failure in %s shader:
%s
", strShaderType, strInfoLog);
		delete[] strInfoLog;
	}
    
	return uiShader;
}

GLuint CreateProgram(const char *a_vertex, const char *a_frag)
{
	std::vector<GLuint> shaderList;
    
	shaderList.push_back(CreateShader(GL_VERTEX_SHADER, a_vertex));
	shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, a_frag));
    
	//create shader program ID
	GLuint uiProgram = glCreateProgram();
    
	//attach shaders
	for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
		glAttachShader(uiProgram, *shader);
    
	//link program
	glLinkProgram(uiProgram);
    
	//check for link errors and output them
	GLint status;
	glGetProgramiv (uiProgram, GL_LINK_STATUS, &status);
	if (status == GL_FALSE)
	{
		GLint infoLogLength;
		glGetProgramiv(uiProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
        
		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
		glGetProgramInfoLog(uiProgram, infoLogLength, NULL, strInfoLog);
		fprintf(stderr, "Linker failure: %s
", strInfoLog);
		delete[] strInfoLog;
	}
    
	for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
	{
		glDetachShader(uiProgram, *shader);
		glDeleteShader(*shader);
	}
    
	return uiProgram;
}


int main(void)
{
    GLFWwindow* window;
    
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(800, 600, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    
    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    
    if (glewInit() != GLEW_OK)
    {
        // OpenGL didn't start-up! shutdown GLFW and return an error code
        glfwTerminate();
        return -1;
    }
    const float vertexPositions[] =
    {
        -0.3f, 0.10f, 0.0f, 1.0f, //height and left of right position
        -0.50f, -0.05f, 0.0f, 1.0f,// right point position of triangle
        -0.10f, -0.05f, 0.0f, 1.0f, // left point position of triangle
        
        0.0f, 0.03f, 0.0f, 1.0f,
        -0.025f, -0.05f, 0.0f, 1.0f,
        0.025f, -0.05f, 0.0f, 1.0f,
    };
    
    const float vertexColours[] =
    {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f,
    };
    
    //create shader program
   GLuint uiProgramFlat = CreateProgram("VertexShader.glsl", "FlatFragmentShader.glsl");


       /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        glClearColor(0.0f,0.0f,0.0f,0.0f);
        
        glClear(GL_COLOR_BUFFER_BIT);
        /* Render here */
        
        //enable shaders
        glUseProgram(uiProgramFlat);
        
        //enable the vertex array state, since we're sending in an array of vertices
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        
        //specify where our vertex array is, how many components each vertex has,
        //the data type of each component and whether the data is normalised or not
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, vertexPositions);
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, vertexColours);
        
        //draw to the screen
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        
        /* Poll for and process events */
        glfwPollEvents();
    }
    
    glfwTerminate();
    return 0;
}

Regards

Theodore.

I have set the a_eshaderType to null and this seems to get rid of the error but no colours are produced?, I also set a_strShaderFile = null and the a_vertex = null and still no colour, there is no longer any error but still no colour, I have used sublime text to change the Encoding and it still doesn’t work.

Im at a loss.

Setting an integer to NULL doesn’t really make sense. Of course nothing works if you set the shader string/filename to NULL, no shader gets loaded. You might have misunderstood arekkusus suggestion. You weren’t supposed to set values to NULL to solve anything, but to actually use a debugger, set a breakpoint in the shader loading routine and check the values of the variables.

Thank you Agent D,

is it possible for you to show me an example, I admit im not the best at programming I have only started to learn the language myself, and I have set break points to see the value of those pointers, but if I could only get one example of what i am looking for or at that would give me a starting point.

Regards
Theodore.

What I had in mind about CreateShader is mostly this (for each shader):
[ul]
[li]Is the file name correct? [/li][li]Can the file be opened? (If not, the loading loop is skipped) [/li][li]What’s the value of szShaderSourcePointer? (is it NULL, does it contain the expected data, …) [/li][li]What’s the value of uiShader? (is it non-zero?) [/li][/ul]

But skimming over your code, I just noticed this in CreateProgram:


...
for( auto shader = shaderList.begin(); shader != shaderList.end(); shader++ )
{
    glDetachShader(uiProgram, *shader);
    glDeleteShader(*shader);
}
...

There is no condition around it. This code is always run at the end of CreateProgram. Why are you doing this? You always remove all shader objects from a program after linking it and delete them, even if it was successfull!?

Hello Agent D,

Thank you for your help and reply, I guess I should let you and others know why I am having difficulty,

The college that supplied the code placed that particular part in, Just to give you a little background, the college AIE do not help students understand the reasons behind the code, its been very frustrating, I have spent most of my time researching the materials myself and asking for help where I can get it. we only just started learning this material, so that you can grasp the full scope of how hard it is, the college skipped from session 4 to session 8, in other words from implementation of code to understanding the math behind it. Very odd, despite the fact students have complained, they seem not to care.

So here we are, attempting things without proper knowledge of the subject, so again ill be honest I do not know why this code is there? its all copy and paste. I have been trying to make sense of a lot of it but its a slow process, this is why I am posting so I may get a better understanding of what is happening. I can read the code and get a fair Idea of what its trying to do but have very little knowledge as to why it is doing it, to put it in other words a leap of faith.

I do thank you for your help and appreciate it greatly, you and all here in the forums, it has been a great relief to find somewhere to get aid.

Regards

Theodore.