<p>It basically <strong>gives <em>you</em> control of the semantics of the individual attribute indices</strong>. This mostly pays off in bigger projects, where you have many different shaders which, however, often use rather common attributes, like e.g. positions or texture coordinates. With explicit attribute locations you can establish a semantic for those attributes and enforce that on <em>all your shaders</em>, e.g. positions are always in attribute 0, texture coordinates always in attribute 1, ...</p>

<p>First, this has the advantage of not having to query the attribute index for <em>every shader</em>. Of course you could query it once and sotre it in e.g. a map from attribute names/semantics to indices, but establishing this beforehand is even better. And it brings us to a much bigger advantage, without explicit attribute binding you <strong>would have to bind vertex arrays of a model to the individual attribute channels not only for each model, but also <em>for each shader</em></strong>, since your position array might go to attribute 0 in one shader and attribute 1 in another one.</p>

<p>This is not only a huge hassle, but also basically <strong>destroys the advantage of <a href="https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object" rel="nofollow noreferrer">Vertey Array Objects (VAO)</a></strong>, which are specfically there to capture all the vertex attribute state, including the enabled attribute indices and their buffer sources so that you can draw your object by just binding the VAO and calling a draw function. Without explicit attribute binding you would <em>still</em> have to reenable/bind the individual arrays at least for each different shader you use. <strong>With explicit attribute binding you know that your model's positions <em>always</em> go into attribute index 0</strong>.</p>

CC BY-SA 4.0
2017-10-01T16:14:37.520Z
2018-08-08T08:35:00.123Z
2018-08-08T08:35:00.123Z
Christian Rau
<p>I'm a beginner in OpenGL and I've been doing some research on the topic of shaders and attributes. Most places I go say that explicit attribute binding (whether it be in the shader itself with <code>layout(location=x)</code> or with <code>glBindAttribLocation</code>) is better than letting OpenGL generate some attribute number. However, I didn't completely understand WHY explicit attribute binding is better.</p>

<p>So I guess my question is, what are the pros/cons to explicit/automatic attribute binding, and which one is more conducive to better program design overall?</p>

<opengl><shader>
Explicit vs Automatic Attribute Binding
Chara
# Explicit vs Automatic Attribute Binding

(Asked by: username_0 on 2016-02-11)

I'm a beginner in OpenGL and I've been doing some research on the topic of shaders and attributes. Most places I go say that explicit attribute binding (whether it be in the shader itself with `layout(location=x)` or with `glBindAttribLocation`) is better than letting OpenGL generate some attribute number. However, I didn't completely understand WHY explicit attribute binding is better.

So I guess my question is, what are the pros/cons to explicit/automatic attribute binding, and which one is more conducive to better program design overall?

**Tags:** opengl, shader


---

## 1 answers

### Answer by username_1 on 2017-10-01. Score: 2

It basically **gives *you* control of the semantics of the individual attribute indices**. This mostly pays off in bigger projects, where you have many different shaders which, however, often use rather common attributes, like e.g. positions or texture coordinates. With explicit attribute locations you can establish a semantic for those attributes and enforce that on *all your shaders*, e.g. positions are always in attribute 0, texture coordinates always in attribute 1, ...

First, this has the advantage of not having to query the attribute index for *every shader*. Of course you could query it once and sotre it in e.g. a map from attribute names/semantics to indices, but establishing this beforehand is even better. And it brings us to a much bigger advantage, without explicit attribute binding you **would have to bind vertex arrays of a model to the individual attribute channels not only for each model, but also *for each shader***, since your position array might go to attribute 0 in one shader and attribute 1 in another one.

This is not only a huge hassle, but also basically **destroys the advantage of [Vertey Array Objects (VAO)](https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object)**, which are specfically there to capture all the vertex attribute state, including the enabled attribute indices and their buffer sources so that you can draw your object by just binding the VAO and calling a draw function. Without explicit attribute binding you would *still* have to reenable/bind the individual arrays at least for each different shader you use. **With explicit attribute binding you know that your model's positions *always* go into attribute index 0**.


---

