Keugyeol
Keugyeol

Reputation: 2435

GLSL Linking fails without useful information

Similar titled question here: GLSL:shader linking fail (but no log) but in my case, both vertex and fragment shaders are very simple and in/out variables match as listed below.

[EDIT] Code for loading the shaders are listed further down. Since VS2010 does not support range-based for-loop, some part of the code are #ifdef'd. But anyway... I have tried the code with MinGW 32bit environment and it links OK.

Shader linking succeeds and runs fine when built with VS2010, but fails with NetBeans + MinGW-w64 and gives this log message:

Link info
---------
No shader objects attached.

Could it be something that's related with MinGW-w64 OpenGL librarys?

Here is my Vertex shader, and

#version 330

in vec4 vPosition;
in vec4 vColor;

out vec4 color;

void main()
{
    color = vColor;
    gl_Position = vPosition;
}

here is my Fragment shader.

#version 330

in vec4 color;
out vec4 fColor;

void main()
{
    fColor = color;
}

LoadShader.h:

typedef struct {
    GLenum       type;
    const char*  filename;
    GLuint       shader;
} ShaderInfo;

main.cpp:

vector<ShaderInfo> shaders;
ShaderInfo vert = {GL_VERTEX_SHADER, "SimpleVertexShader.vert"};
ShaderInfo frag = {GL_FRAGMENT_SHADER, "SimpleFragmentShader.frag"};
shaders.push_back(vert);
shaders.push_back(frag);

program = LoadShaders(shaders);

LoadShader.cpp - LoadShaders()

GLuint LoadShaders(vector<ShaderInfo> shaders)
{
    if (shaders.empty()) return 0;

#if !defined(_MSC_VER) || 1600 < _MSC_VER  
    for (auto entry : shaders)
        entry.shader = CreateShader(entry.type, entry.filename);
#else
    for (vector<ShaderInfo>::iterator entry = shaders.begin(); entry != shaders.end(); ++entry)
        entry->shader = CreateShader(entry->type, entry->filename);
#endif
    // Create the program
    return CreateProgram(shaders);
}

LoadShader.cpp - CreateShader()

GLuint CreateShader(GLenum shaderType, const char* shader_file_path)
{
    // Create the shader
    GLuint shaderID = glCreateShader(shaderType);
    if (!shaderID)
        return 0;

    // Read the shader code from the file
    std::string shaderCode;
    std::ifstream shaderStream(shader_file_path, std::ios::in);
    if(shaderStream.is_open())
    {
        std::string Line = "";
        while(getline(shaderStream, Line))
            shaderCode += "\n" + Line;
        shaderStream.close();
    }

    // Compile the shader
    printf("Compiling shader : %s\n", shader_file_path);
    char const* sourcePointer = shaderCode.c_str();
    glShaderSource(shaderID, 1, &sourcePointer , NULL);
    glCompileShader(shaderID);

    // Check the shader
    GLint compiled;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        GLsizei len;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &len);

        GLchar* log = new GLchar[len+1];
        glGetShaderInfoLog(shaderID, len, &len, log);
        std::cerr << "Shader compilation failed: " << log << std::endl;
        delete [] log;

        return 0;
    }

    return shaderID;
}

LoadShader.cpp - CreateProgram()

GLuint CreateProgram(vector<ShaderInfo> shaders)
{
    // Create and link the program
    fprintf(stdout, "Linking program\n");
    GLuint programID = glCreateProgram();
    if (!programID)
        return 0;

    // attach shaders and link the program
#if !defined(_MSC_VER) || 1600 < _MSC_VER  
    for (auto iter : shaders)
        glAttachShader(programID, iter.shader);
#else
    for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter)
        glAttachShader(programID, iter->shader);
#endif
    glLinkProgram(programID);

    // Check the program
    GLint linked;
    glGetProgramiv(programID, GL_LINK_STATUS, &linked);
    if (!linked) {
        GLsizei len;
        glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &len);

        GLchar* log = new GLchar[len+1];
        glGetProgramInfoLog(programID, len, &len, log);
        std::cerr << "Shader linking failed: " << log << std::endl;
        delete [] log;

#if !defined(_MSC_VER) || 1600 < _MSC_VER  
        for (auto iter : shaders) {
            glDeleteShader(iter.shader);
            iter.shader = 0;
        }
#else
        for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter) {
            glDeleteShader(iter->shader);
            iter->shader = 0;
        }
#endif

        return 0;
    }

    return programID;
}

Upvotes: 0

Views: 585

Answers (1)

GuyRT
GuyRT

Reputation: 2917

I think you have an error in LoadShaders. The C++ 11 version of the loop:

for (auto entry : shaders)
    entry.shader = CreateShader(entry.type, entry.filename);

takes a copy of each entry in shaders, so the elements in the vector will not be updated. Try instead:

for (auto &entry : shaders)
    entry.shader = CreateShader(entry.type, entry.filename);

By the way, if you want to support older compilers, You might as well just have the old version of the loop.

Upvotes: 3

Related Questions