Performance gain using interleaved attribute array

2020-02-25 22:37发布

问题:

I work with OpenGL4.X .Recently I read this Apple OpenGLES2 doc where it is stated that using interleaved attribute arrays improves performance on IOS mobile devices and is the recommended way (instead of using blocks of attributes).

For those who didn't understand what I mean here is an example:

Block of attributes in a single attribute array:

 float vertices[]{
 //Triangle vertices:

  v0x , v0y , v0z ,
  v1x , v1y , v1z ,
  v2x , v2y , v2z ,

  //Triangle UVs:

  uv0s , uv0t ,
  uv1s , uv1t ,
  uv2s , uv2t ,

  //Triangle Normals:
  n0x , n0y , n0z ,
  n1x , n1y , n1z ,
  n2x , n2y , n2z 

}

Interleaved attribute array:

 float vertices[]{


  v0x , v0y , v0z ,
  uv0s , uv0t ,          ////vertex 1 attributes
  n0x , n0y , n0z ,

  v1x , v1y , v1z ,
  uv1s , uv1t ,         ///vertex 2 attributes
  n1x , n1y , n1z ,

  v2x , v2y , v2z ,
  uv2s , uv2t ,         ///vertex 3 attributes
  n2x , n2y , n2z 

}

So my question is : Is it also true for OpenGL running on desktop GPUs ? If yes ,then how big the performance gain can theoretically be ?

回答1:

Is it also true for OpenGL running on desktop GPUs ?

From Vertex specification wiki page :

As a general rule, you should use interleaved attributes wherever possible. Obviously if you need to change certain attributes and not others, then interleaving the ones that change with those that don't is not a good idea.


how big the performance gain can theoretically be ?

I can't really answer that, but I wouldn't expect huge improvement. The only sure way is to measure.



回答2:

The benefit of interleaved attribute arrays is memory locality. This means that all necessary vertex data is located next to each other and could be fetched more efficiently compared to data located in multiple buffers.

Having big number of vertices with many attributes might manifest the difference in performance. The values of big and many should be established by profiling.



回答3:

In order for any optimization to be a performance gain, it must first optimize something that is a performance bottleneck. Unless it is currently a bottleneck, then doing anything about it will not necessarily improve performance.

There is no way to answer your question because any performance gain first depends on whether you are bottlenecked on vertex transfer performance (ie: what this optimizes). Unless you are actually pushing your graphics card so hard that your vertex shader, fragment shader, and CPU issues don't become bottlenecks, this won't matter.

And there's no way to know how much of a gain it is, because different hardware will respond differently. Different situations will respond differently based on how tight the bottleneck is.

Just interleave your attributes. It costs you nothing, requires minimal time or effort, and may be of non-trivial value performance-wise.



回答4:

I might be wrong, but my perception is that the GPU needs the data (vertices, normals, and uv maps) when rendering say a vertex of a triangle and if the buffer for vertices, normals, and uvmaps is large for an object e.g. a large sphere (rendered with glvertex not glsphere)...

The GPU has to go back and forth for vertices, normals, and uvmaps while rendering a small rectangle because it can't store all of those in a buffer inside itself.

Communication over the bus is generally slower than the processor speed.

Now, in this case interleaved arrays are a great a gain and reduce bus communication and the GPU can easily process interleaved arrays and will have all the data available for a particuler set of vertices being rendered.