I want to query metadata for a given attribute. I hope I have misunderstood how glGetActiveAttrib
works. Here's what I think how it works:
The attribute location can be obtained using glGetAttribLocation(programId, attributeName)
. Metadata can be obtained using glGetActiveAttrib(programId, index, ...)
.
As you can see, glGetActiveAttrib
expects an index instead of a location.
This is not the same.
In the Shader:
attribute vec3 position;
attribute vec3 textureCoordinate; // <-- this attribute is not used
attribute vec3 normal;
In this example, the attribute locations will be
locations = [
position: 0,
textureCoordinate: -1, // optimized away
normal: 2, // continues counting the attributes
]
However, the active attribute indices will be
active_attribute_indices = [
position: 0,
// skips texture coordinate because it is not active
normal: 1,
]
As you can see, the following will not work:
// get attribute location by name
int attrib_location = glGetAttribLocation(programId, "normal"); // = 2
// get attribute metadata
// Error: attrib_location being 2 is not a valid active attribute index.
glGetActiveAttrib(programId, attrib_location, ...)
Therefore, my question is:
How can I get the index of an active attribute, not the location?
Do I have to loop through all attributes and check whether the name matches the name of my attribute?
Under the old introspection API, there is no way to retrieve an attribute index by name. So you will have to loop through the attribute list to find it.
Under the more modern introspection API (available to GL 4.3 and via extension), you can query any named resource index by name (assuming your shader is not a SPIR-V shader) via glGetProgramResourceIndex
. For vertex shader inputs, you pass the interface GL_PROGRAM_INPUT
.