openglgraphics3d

What's the point of having a single vertex attribute?


I notice that the size parameter for gl.vertexAttribPointer() can be 1, 2, 3, or 4. What is the point of having 1 attribute per vertex?

Is it for rendering lines? If that's the case, then wouldn't the lines have to be one dimensional?

This also seems to be allowed in Vulkan.


Solution

  • Every GPU has a certain number of attributes. An attribute is a small bit of memory which (more or less) can store 4 floats. In older OpenGL versions the GPU has to contain at least 8 attributes, in newer ones at least 16, but of course, it can be more. You can get the actual number if you call glGetIntegerv, with GL_MAX_VERTEX_ATTRIBS.

    The function vertexAttribPointer tells the GPU what data should be stored in an attribute for each vertex shader invocation. Almost always there is a vertex attribute that contains the position of the vertex just like you mentioned. The vertex's position is usually 3 floats, or 2 if you draw in 2D. However, as I mentioned earlier, you can have multiple attributes, and you can decide what data you want to use with those attributes. Normal vectors (3 floats), and texture coordinates (2 floats) are common things to store in attributes, but you can store any data you want (as long as it's not larger than 4 floats).

    So even if you draw lines, the size of the position will be 2 or 3, because as you said 1 float is not enough to tell where a point is. The size of 1 is not as frequently used, but if you have some data that is only 1 float per vertex, you can use it. However, keep in mind, that regardless of how many floats (1-4) you use, the attribute as a piece of hardware remains the same. So if you use only 1 float, you pretty much wasted the other 3. So if you want efficient rendering you should pack your data into the fewest number of attributes possible because the number of attributes is limited, and as you use more and more attributes you will use more and more bandwidth.