c++openglvertexvertex-attributes

Should the indices of glVertexAttribPointer() be contiguous?


glVertexAttribPointer()'s first argument is an index representing where the vertex attribute resides, Opengl offers a minimum of 16 slots.

I want all 16 to represent different vertex attributes (position=0, uvs=1, colors=3, weights=8, etc) and for these to be constant across every vertex type, even if the vertex doesn't use that attribute.

My question is, does it matter at all if the indices are not contigious for a given vertex? For example, would this be inefficient at all:

enum Attrib : GLuint {
    aPos        = 0,
    aUv1        = 1,
    aUv2        = 2,
    aColor1     = 3,
    aColor2     = 4, 
    aNormal     = 5,
    aTangent    = 6,
    aBitangent  = 7,
    aBoneIndex  = 8,
    aBoneWeight = 9,
    aAttrib1    = 10,
    aAttrib2    = 11,
    aAttrib3    = 12,
    aAttrib4    = 13, 
    aAttrib5    = 14,
    aAttrib6    = 15, 
    a__count    = 16
};

glVertexAttribPointer(aPos, ...);
glVertexAttribPointer(aUv1, ...);
glVertexAttribPointer(aAttrib1, ...);

Solution

  • There is no known performance penalty from using arbitrary attribute indices for vertex arrays. To add some justification to this, more recent APIs like Vulkan and D3D12 had the opportunity to enforce contiguity of attribute indices. Being more low-level APIs, if there was some performance to be gained by such a restriction, they would have imposed it.

    They did not.

    By contrast, Vulkan effectively made the vertex format part of the program pipeline state, thus enforcing a direct correlation between programs and the vertex format. So it's not like Vulkan wasn't willing to restrict things compared to OpenGL.