Say I have a model made up of vertices, each with position, normal, tangent, and texcoord attributes, where triangles are specified by index triples.

If we just focus on the vertex attributes, I'm aware of two broad strategies: the structure of arrays, and the array of structures. I've also heard that the array of structures is preferred because it increases the memory locality (and therefore cache locality) of the attributes for a given vertex.

Is it really the case that this improves performance? The main way I could think that this would come about is by vertex indices that require the rasterizer to acquire vertex data that has long since been evicted from cache. If vertex data access is random like this, then keeping all the attributes for a vertex on the same cache line would certainly make things faster, but is this not an issue that could be mostly mitigated by optimizing the order of triangle specification?

Furthermore, it's my understanding that modern GPUs may be better at unpacking long vectors of the same type, than vectors of structures of many types. Would it then be possible for a structure-of-arrays layout to consistently outperform an array-of-structures layout of the same vertex data, if the index order is optimized?

  • 3
    $\begingroup$ Couldn't you simply try it out and see which is fastest for your case? $\endgroup$ – user1118321 Mar 19 '18 at 2:20
  • $\begingroup$ I would have thought for indexed meshes that array-of-structures would be more efficient as the vertex shader will be fed all vertex attributes so having them grouped together in memory is going to be cache friendlier. $\endgroup$ – PaulHK Jan 11 at 7:33
  • $\begingroup$ Depending on the data types in structure, vec4s and floats pack together nicely, other types not so well $\endgroup$ – PaulHK Jan 11 at 7:41
  • 1
    $\begingroup$ @user1118321 While individual benchmarking experiments are a commendable practice, there certainly is more long-term value in a broader theoretical discussion and maybe resulting establishment of general practices based on how vertex pulling hardware works. $\endgroup$ – Christian Rau Feb 26 at 9:31

This may depend on target hardware and the API you are going to use. Can you provide more info? Here's some (very broad and general) best practices for OpenGL. https://www.khronos.org/opengl/wiki/Vertex_Specification_Best_Practices

Also, do you have a performance problem? Or are you just curious.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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