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?

  • 1
    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... – Daniel M Gessel Feb 11 '16 at 5:21
  • The accepted answer on this post argues for the pro-explicit side, maybe you could take a look at why he thinks so. stackoverflow.com/questions/4635913/… – Chara Feb 17 '16 at 19:30
  • 1
    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... – Daniel M Gessel Feb 18 '16 at 2:01

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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