Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Summary of best VBO practices


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 jmakitalo   Members   -  Reputation: 591

Like
0Likes
Like

Posted 20 January 2013 - 04:09 AM

I have read several articles about using VBO:s and VAO:s on today's hardware, but due to mixed information, I'm not sure what the bottom line is. For example http://www.opengl.org/wiki/VBO_-_more is instructive, but I feel that someone could have second opinions on these matters.

 

In my game, I usually have models with approximately 300-1000 polygons and 1-3 textures. Each model also has a few lower detail versions of them, which don't use the same vertex data, but usually do use the same textures. Additionally, some models (e.g. player) can have 20k polygons and maybe 5 textures.

 

In my current implementation, a mesh is divided into groups by texture. Each group has VBO:s for vertex positions, normals, texture coordinates etc. (each has its own VBO) and then an index buffer. After culling, I have a bunch of mesh groups, which I then first sort by texture and then by VBO and draw by glDrawElements. The vertex data is always static and all animating is done in shaders.

 

A few questions:

 

1)

If a group has, say 4000 polygons (which could happen for the player models), is it advisable to call glDrawElements once for the whole chunk, or should I cut it into pieces? I read about some "cache pressure" kicking in with large chunks, but I don't understand what it means. I have experienced some hick ups with 20k models, but just dividing the draw calls didn't seem to help.

 

2)

Should I put all vertex data to a single VBO? For each group or the whole mesh? I read that 1 - 4 MiB buffer is preferred on some hardware, so should I go further and implement some sort of general VBO allocator, so that the data of several meshes are pushed there? For 4 MiB VBO and 1000 polygon meshes, I can see how this would reduce bindings, if all meshes use the same types of attributes.

 

3)

Should I use interleaved arrays? I remember that there has been some controversy with this issue. Probably depends on local coherence of vertex / index data.

 

4)

Any other considerations?



Sponsor:

#2 Katie   Members   -  Reputation: 1375

Like
3Likes
Like

Posted 20 January 2013 - 12:03 PM

"Should I put all vertex data to a single VBO?"

 

Whereever possible, yes. It means the whole vertex will be pulled into a cache at the same time. By having multiple VBOs, you're using multiple memory units which means multiple caches need to be loaded. Each will contain the vertex data for more vertices, but that's not necessarily useful; if you use the coords for a vertex, you'll use the normal data and probably quite soon, so the memory effort in loading it is useful.

 

If you have multiple memory units loading many verts into their cache, their effort may well be wasted -- touching vertex V implies nothing about when or even whether you will need the data for vertex V+1.

 

"If a group has, say 4000 polygons (which could happen for the player models), is it advisable to call glDrawElements once for the whole chunk, or should I cut it into pieces?"

 

Do the whole thing. Reason; let the driver do the optimisation work. It knows what shape the hardware is, and you don't. Don't try and second guess it unless you have a known crap driver you're trying to work round. Some drivers, for example, may take the min/max vert index and transform everything in that range and then bin the unused values. If you unchunk the data, they'll obviously waste more time on unused nodes. For the same sorts of reasons, try and make sure all the verts in a chunk are adjacent in your VBO.

 

You're also likely to transform verticies on the split boundaries more than once, whereas as one draw, they'll likely only be done once.



#3 jmakitalo   Members   -  Reputation: 591

Like
0Likes
Like

Posted 20 January 2013 - 01:34 PM

"Should I put all vertex data to a single VBO?"

Whereever possible, yes. It means the whole vertex will be pulled into a cache at the same time. By having multiple VBOs, you're using multiple memory units which means multiple caches need to be loaded. Each will contain the vertex data for more vertices, but that's not necessarily useful; if you use the coords for a vertex, you'll use the normal data and probably quite soon, so the memory effort in loading it is useful.

If you have multiple memory units loading many verts into their cache, their effort may well be wasted -- touching vertex V implies nothing about when or even whether you will need the data for vertex V+1.

"If a group has, say 4000 polygons (which could happen for the player models), is it advisable to call glDrawElements once for the whole chunk, or should I cut it into pieces?"

Do the whole thing. Reason; let the driver do the optimisation work. It knows what shape the hardware is, and you don't. Don't try and second guess it unless you have a known crap driver you're trying to work round. Some drivers, for example, may take the min/max vert index and transform everything in that range and then bin the unused values. If you unchunk the data, they'll obviously waste more time on unused nodes. For the same sorts of reasons, try and make sure all the verts in a chunk are adjacent in your VBO.

You're also likely to transform verticies on the split boundaries more than once, whereas as one draw, they'll likely only be done once.
Ok, thanks for your insight. And I guess that interleaving is useful, if all attributes are used, but maybe not so if only e.g. position data is used (like for shadowmaps).

I'm thinking of writing a VBO manager that is passed to mesh classes and such to unify vertex data management. I was thinking of writing two classes: CVertexArrayObject to store one vbo for attributes, a vertex array object and one index buffer, and CVertexArrayManager, which would create new vertex arrays whenever a buffer size exceeds some threshold. Something like
class CVertexArrayObject
{
private:
  GLuint vaoID;
  GLuint vboID;
  GLuint indexID;
// ...
};

class CVertexArrayManager
{
private:
  std::vector<CVertexArrayObject*> objects;

  // ...

public:
  CVertexArrayManager();
  ~CVertexArrayManager();

  // Maximum buffer sizes.
  void setVertexBufferSize(int _vbSize);
  void setIndexBufferSize(int _ibSize);

  // Request new buffer, which is identified by index to object
  // and offset within the buffer of the object.
  bool allocateVertexBuffer(int size, int &objectIndex, int &offset);
  bool allocateIndexBuffer(int size, int &objectIndex, int &offset);

  // Bind object for rendering (vertex attribute buffers and index buffer).
  void bindObject(int objectIndex);

  // Write index data to allocated buffer.
  void fillIndexBuffer(int objectIndex, int offset, GLushort *indices, int nindices);

  // Write vertex data to allocated buffer.
  // Data is a struct array containing interleaved data.
  template <class T>
  void fillVertexBuffer(int objectIndex, int offset, const T *data, int size);
};

An instance of the manager would then be passed to a mesh class, which would fill the buffers with its data. What do you think?

Edited by jmakitalo, 20 January 2013 - 02:34 PM.


#4 mhagain   Crossbones+   -  Reputation: 8284

Like
0Likes
Like

Posted 20 January 2013 - 07:00 PM

Ok, thanks for your insight. And I guess that interleaving is useful, if all attributes are used, but maybe not so if only e.g. position data is used (like for shadowmaps).

 

Interleaving may still be useful even then.  It may mean that you get to reuse the same VBO (and potentially even the same vertex shader) for your shadowmapping pass as you use for your regular passes, which may be a win, but it all depends on the architecture of your renderer.  True, there will be attributes unused during that pass, but the benefit from not having to switch buffers, or not having to use a slower path for your main render, may well outweigh the cost of having some extra attribs for a shadowmapping pass.

 

It may also give you a benefit in code cleanliness which - even if it does turn out slower - you may well feel is worthwhile.

 

Katie made the point that you shouldn't try to second-guess the driver, and this is so true.  When you're writing this kind of code you are no longer in the software-only realm, and things that you think may make sense to do might actually turn out to be the worst possible choice.  That, to me, is the single most important point here - if you're of a mindset that "doing X is better because it saves memory", etc, you need to shake that mindset off quite quickly.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 jmakitalo   Members   -  Reputation: 591

Like
0Likes
Like

Posted 21 January 2013 - 07:11 AM

Katie made the point that you shouldn't try to second-guess the driver, and this is so true. When you're writing this kind of code you are no longer in the software-only realm, and things that you think may make sense to do might actually turn out to be the worst possible choice. That, to me, is the single most important point here - if you're of a mindset that "doing X is better because it saves memory", etc, you need to shake that mindset off quite quickly.

 

Yes, this sounds probable. Although it seems to me that the best way to go in this sort of things tends to change over time.

 

I also read that calling glVertexAttribPointer is very expensive. I would guess that if I can squeeze about 100 meshes into a single VBO, it would make sense to first sort by VBO and then by texture, although texture binds are also expensive. Having only a few large VBO:s is also probably good from the point of view of LOD. If each mesh would have its own VBO, then adding levels of detail would increase the number of buffer binds, which could diminish the benefit of having LOD in the first place.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS