'OpenGL Shader binaries not linking in C++

I am currently working on a OpenGL Shader class, that uses SPIR-V to compile the shaders. Getting the compiled binaries from SPIR-V works fine, the problem is, that my program crashes with the OpenGL exception GL_INVALID_OPERATION error generated. <program> has not been linked, or is not a program object. when trying to bind the Shader with glUseProgram(m_RendererID).

This is my shader creation function:

void OpenGLShader::LoadAndCreateShaders(const std::unordered_map<GLenum, std::vector<uint32>> &shaderData)
{
    if (m_RendererID)
        glDeleteProgram(m_RendererID);

    GLuint program = glCreateProgram();
    m_RendererID = program;

    std::vector<GLuint> shaderRendererIds;
    shaderRendererIds.reserve(shaderData.size());

    for (auto &[stage, data] : shaderData)
    {
        GLuint shaderId = glCreateShader(stage);
        glShaderBinary(1, &shaderId, GL_SHADER_BINARY_FORMAT_SPIR_V, data.data(), (uint32)data.size());
        glSpecializeShader(shaderId, "main", 0, nullptr, nullptr);
        glAttachShader(program, shaderId);

        GLint status;
        glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
        if (status == GL_FALSE)
        {
            std::cout << "Shader compilation failed" << std::endl;
        }

        shaderRendererIds.emplace_back(shaderId);
    }

    // Link shader program
    glLinkProgram(program);

    int32 isLinked = 0;
    glGetProgramiv(program, GL_LINK_STATUS, (int32*)&isLinked);
    if (isLinked == GL_FALSE)
    {
        int32 maxLength = 0;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);

        if (maxLength > 0)
        {
            std::vector<GLchar> infoLog(maxLength);
            glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
            std::cout << "Shader linking failed: " << &infoLog[0] << std::endl;

            glDeleteProgram(program);
            for (auto id : shaderRendererIds)
                glDeleteShader(id);
        }
    }

    for (auto id : shaderRendererIds)
        glDetachShader(program, id);
}

This function basically takes in the compiled sources from SPIR-V and tries to create the OpenGL shader. When I debugged the program, I found out that after glLinkProgram() isLinked stays 0, but the maxLength variable stays also 0. So the linking process failed, but didn't give an error. Did anyone have the same issue before?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source