RandyGaul
RandyGaul

Reputation: 1915

glBindAttribLocation, name parameter non-existent in shader

I've been reading this question and the accepted answer here: Explicit vs Automatic attribute location binding for OpenGL shaders

I've stored a hard-coded array of strings to represent available attributes for vertex shaders to use. During the loading of a shader I specify attribute locations like this:

for ( int i = 0; i < Attribute::eCount; ++i )
{
    const char* name = attributeTable.Find( i );

    glBindAttribLocation( program, i, (const GLchar*)name );
}

I'm wondering if it's appropriate to call glBindAttribLocation when the provided name parameter does not exist anywhere in the shader.

If we read here: https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindAttribLocation.xml, it states "It is also permissible to bind a generic attribute index to an attribute variable name that is never used in a vertex shader."

It sounds like it's okay to call glBindAttribLocation if an attribute is defined in the shader but not used. I want to know if it's okay if the attribute doesn't exist at all. My hunch is that it's fine since the glsl compiler aggressively removes unused code, but I can't seem to verify this.

Upvotes: 2

Views: 992

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54602

Calling glBindAttribLocation() for a non-existent attribute is perfectly legal, and not an error. This is specifically mentioned in the OpenGL spec. The following is from page 365 of the OpenGL 4.5 spec, in section 11.1.1 "Vertex Attributes":

BindAttribLocation may be issued before any vertex shader objects are attached to a program object. Hence it is allowed to bind any name to an index, including a name that is never used as an attribute in any vertex shader object. Assigned bindings for attribute variables that do not exist or are not active are ignored.

Upvotes: 2

Related Questions