Search code examples
c++openglglfw

glGetActiveAttribute() Reading The Wrong Attribute Index?


Minimal reproducible example:

#include <iostream>
#include "glad.h"
#include <glfw3.h>

int main(int argc, char* args[])
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    auto window = glfwCreateWindow(800, 600, "Example", NULL, NULL);
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        throw new std::runtime_error("Failed to initialize GLAD");

    const char* vertexSrc = "#version 330 core\n"
        "in vec3 APos;\n"
        "in vec3 VertCol_In;\n"
        "out vec3 VertCol;\n"
        "in vec2 TexCoord_In;\n"
        "out vec2 TexCoord;\n"
        "void main()\n"
        "{\n"
        "   gl_Position = vec4(APos.x, APos.y, APos.z, 1.0);\n"
        "   VertCol = VertCol_In;\n"
        "   TexCoord = TexCoord_In;\n"
        "}\0";

    auto vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSrc, NULL);
    glCompileShader(vertexShader);

    const char* fragSrc = "#version 330 core\n"
        "out vec4 FragColor;\n"
        "in vec3 VertCol;\n"
        "in vec2 TexCoord;\n"
        "void main()\n"
        "{\n"
        "   FragColor = vec4(VertCol.x,TexCoord.xy, 1.0f);\n"
        "}\0";

    auto fragShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragShader, 1, &fragSrc, NULL);
    glCompileShader(fragShader);

    auto prog = glCreateProgram();
    glAttachShader(prog, vertexShader);
    glAttachShader(prog, fragShader);
    glLinkProgram(prog);

    auto aPos_layout = glGetAttribLocation(prog, "APos");
    auto vertCol_in_layout = glGetAttribLocation(prog, "VertCol_In");
    auto texCoord_in_layout = glGetAttribLocation(prog, "TexCoord_In");

    GLenum type;
    GLsizei length;
    GLint size;
    const GLuint bufSize = 16;
    GLchar bufA[bufSize];
    GLchar bufB[bufSize];
    GLchar bufC[bufSize];

    glGetActiveAttrib(prog, aPos_layout, bufSize, &length, &size, &type, bufA);
    std::cout << "Name at aPos_layout ("<<aPos_layout<<"): " << bufA << "\n";

    glGetActiveAttrib(prog, vertCol_in_layout, bufSize, &length, &size, &type, bufB);
    std::cout << "Name at vertCol_in_layout ("<<vertCol_in_layout<<"): "<<bufB << "\n";

    glGetActiveAttrib(prog, texCoord_in_layout, bufSize, &length, &size, &type, bufC);
    std::cout << "Name at texCoord_in_layout ("<<texCoord_in_layout<<"): " << bufC << "\n";

    return 0;
}

As far as I understand how glGetActiveAttrib() and glGetAttribLocation() work, the output for executing this SHOULD be:

Name at aPos_layout (0): APos
Name at vertCol_in_layout (1): VertCol_In
Name at texCoord_in_layout (2): TexCoord_In

However, it is not. The output is instead:

Name at aPos_layout (0): APos
Name at vertCol_in_layout (1): **TexCoord_In**
Name at texCoord_in_layout (2): **VertCol_In**

Note how the names at vertCol_in_layout and texCoord_in_layout have swapped. Whilst obviously I already know the attribute names I'm getting an index from in this MRE, the point I'm trying to illustrate here is that either glGetAttribLocation()'s return value or glGetActiveAttribute()'s 2nd parameter aren't acting according to documentation.

From glGetAttribLocation()'s documentation:

Parameters

program
Specifies the program object to be queried.

index
Specifies the index of the attribute variable to be queried.

From glGetActiveAttribute()'s documentation:

glGetAttribLocation queries the previously linked program object specified by program for the attribute variable specified by name and returns the index of the generic vertex attribute that is bound to that attribute variable

Is this just a bug in GLFW/OpenGL? I can't figure how this would be happening otherwise since the intent in the documentation is pretty clear. That or something obvious is going wrong that I'm somehow blind-sighting and as someone pretty new to OpenGL/GLFW I'd be willing to believe that.


Solution

  • There is no conflict in the output, as the index (as used in glGetActiveAttrib) and the location of an active attribute are not the same thing. The OpenGL documentation calls the value of a location an "index" as well, which can make this confusing.

    In the code provided, first the location of the attribute retrieved, which is then used as the index for glGetActiveAttribute. Please also see this answer, quoting Nicol Bolas: "Under the old introspection API, there is no way to retrieve an attribute index by name." In other words, what the code is trying to do, is impossible.


    Index, as related to vertex shader introspection

    From the documentation:

    The index​ is a number on the half-open range [0, GL_ACTIVE_ATTRIBUTES).

    For typical vertex shader introspection, first glGetProgramiv is called with GL_ACTIVE_ATTRIBUTES to retrieve the total number of active attributes, and then glGetActiveAttrib is called in a loop where index ranges over the half-open range above.

    Location

    The location of an attribute is related to how the data is laid out, and is related to functions as glBindAttribLocation, glGetAttribLocation, glVertexAttribPointer, etc. One attribute (with a unique index as above) can take up multiple locations (e.g. for a mat4 attribute). The location of an attribute can also be specified in the shader code itself.