8
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ In principle, I think automatic has an advantage: when using the same vertex shader with a variety of fragment shaders, some attributes may not propogate to the output of a given pixel shader and the compiler can detect this and inform the application, allowing optimization of the vertex layout. In practice, I don't take advantage of this (for a few reasons). I too am curious to hear what the downside to automatic is... $\endgroup$
    – user2500
    Feb 11, 2016 at 5:21
  • 1
    $\begingroup$ Reusing VAOs with multiple shaders that use the same vertex layout. If you let the compiler decide, you have potentially need a VAO for each VB for each compiled program, even if they use exactly the same attributes. Sounds good to me! I should look up what Vulkan does, now that the spec is out... $\endgroup$
    – user2500
    Feb 18, 2016 at 2:01

1 Answer 1

2
$\begingroup$

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), 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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.