Possible Duplicate:
Explicit vs Automatic attribute location binding for OpenGL shaders
In another question of mine one of the answers indicate I should use glBindAttribLocation and not allow the shader compiler to assign its own indices. I’m wondering why this is a recommended practice, what advantages does it have (or rather what disadvantages does not using it have)?
I do understand that if I have multiple programs which share attributes this makes sense, since I can switch between programs and not have to reset those attributes. However, for attributes which aren’t shared, or if my programs just don’t use such sharing, I don’t see that it is necessary to bind the index explicitly.
First of all I don’t think it’s a general recommendation to use explicit binding via
glBindAttribLocationinstead ofglGetAttribLocation. That being said, these are the main reasons I stopped usingglGetAttribLocation:First of all, this can cause unnecessary overhead. It all seems nice and well that you can use a readable names instead of numbers. ‘What the heck is attribute 7’ vs ‘oh right, attribute texture_coordinate’: I’ll explain first what the possible overhead can be and then why that last part doesn’t even make sense.
If you need the attribute location often, the overhead of calling
glGetAttribLocationcan become non-negligible, depending on the driver. So to handle the general case you have to build a caching-system. Great, there goes my easy and readable method with names instead of numbers, I just had to write quite a lot of non-trivial wrapper code. Worse, you should really take care that you destroy the cache when the program becomes invalid, it’s quite likely you’ll do this wrong and end up with bugs. So we went from ‘nice readable names’ to ‘horrible buggy mess’.Even more, the ‘nice readable names’ argument is flawed. You can perfectly do something like the following for locations defined in the shader itself:
Or you can also use an associative container like this:
This one you can also combine with
glBindAttribLocation, just do this prior to linking:Still very readable, no dynamic cache necessary, just some static variables that even might get optimized away.
Then, you say it yourself: it obviously has it advantages when using multiple programs, I won’t repeat it here, because Kos explained that one in detail already. I’ll just answer one of your arguments:
Attributes that don’t need sharing: This implies there are also attributes that need sharing, for which it is a big advantage that you use fixed locations. Why would you mix two location management approaches in one application? Keep yourself from a maintenance headache and stick with one, here obviously the predefined locations because you need them for the shared attributes.