Search code examples
c++opengloptimizationvertex-shader

Is there a penalty for binding attribute locations, if they are unused in a shader?


I have a lot of vertex shaders which use different vertex components. Some handle pretty huge vertices [position, 4 uv channels, 2 color channels, normals, bone weights] while others use small vertices [position and normals only for example].

Can I set (with glBindAttribLocation()) every individual component to use the same index across every shader, even if that attribute doesn't exist in the shader, without a performance penalty?

For example:

glBindAttribLocation(programID, 0, "p");
glBindAttribLocation(programID, 1, "uv");
glBindAttribLocation(programID, 2, "uv1"); // these are used only in certain shaders
glBindAttribLocation(programID, 3, "uv2");
glBindAttribLocation(programID, 4, "uv3");
glBindAttribLocation(programID, 5, "n");   // commonly used across most shaders
...

My worry is if a shader uses few components, but the unused components are active, or the used components are at non-contagious indices, that shader will have to pay for the components it's not using.


Solution

  • You can pass whatever you want to glBindAttribLocation. All that does is define what those locations will be if VS inputs match those names. You can even assign the same location to multiple different names, so long as both names do not appear in the shader.

    The resulting compiled shader will not be affected by extraneous binds.

    That being said, you really ought to put that stuff in the shader itself with layout(location = #) syntax.