Explicit vs Auto-Attribute Location Attributes for OpenGL Shaders

When setting the location of attributes for the OpenGL shader program, you come across two options:

glBindAttribLocation () before referencing an explicit attribute location.

or

glGetAttribLocation () after the link to get the automatically assigned attribute location.

What is a utility for using one over the other?

And which one, if any, is preferable in practice?

+65
opengl-es shader
Jan 08 '11 at 20:16
source share
3 answers

I know one good reason to prefer an explicit location.

Note that geometry data is stored in vertex array objects. For this object, you create a VAO so that the indices match, for example:

  • index 0 : position,
  • index 1 : normals,
  • index 2 : texcoords

Now consider that you want to draw a single object using two different shaders . For one shader, a position and normal data are required, and the rest - positions and texture coordinates .

If you compile these shaders, you will notice that the first shader expects the positions in the attribute index 0 and normals to be 1. Others will expect positions at 0, but texture coordinates at 1.

Quote https://www.opengl.org/wiki/Vertex_Shader :

Automatic assignment

If neither of the two previous methods assigns an input to the attribute index, then the index is automatically assigned to OpenGL when the program is connected. The specified index is completely arbitrary and may be different for different related programs, even if they use the exact vertex shader code.

This means that you cannot use your VAO with both shaders. Instead of having one VAO on, say, an object, you need - in the worst case - a separate VAO for each object per shader .

Forcing shaders to use your attribute numbering convention through glBindAttribLocation can solve this problem easily - all you have to do is keep a consistent relationship between the attributes and their set identifiers and force the shaders to use this convention when stitching.

(This is not a very big problem if you are not using separate VAOs, but you can still make the code more understandable.)




BTW:

When setting the location of attributes for the OpenGL shader program, you come across two options

There is a third option in OpenGL / GLSL 3.3: Indicate the location directly in the shader code . It looks like this:

 layout(location=0) in vec4 position; 

But this is not in the shader language GLSL ES.

+77
Jan 9 2018-11-11T00:
source share

Another answer is that glGetAttribLocation returns data to the caller, which means that it implicitly requires a flash pipeline. If you name it immediately after compiling your program, you essentially force asynchronous compilation to execute synchronously.

+16
Feb 19 '13 at 21:04 on
source share

The third option, i.e. layout(location=0) in vec4 position; in shader code, now available in OpenGL ES 3.0 / GLSL 300 es. Only for input variables of the vertex shader.

+5
Jun 30 '14 at 7:40
source share



All Articles