Tuesday, June 26, 2012

How to count vertices in VideoGames

Ok this one is quite common, but still actual.

Let's say a vertex contains a position, a normal, an 2 UVs. It weights 40 bytes in video memory.
A triangle is just about indices, it weights 6 bytes (2 bytes * 3 indices)

Note that these numbers are here only for comparing examples. Positions, normals, UVs can be compressed (half values) to take less space in video memory. Also stripping of the faces can modify these numbers.

In this example, I have
9 vertices + 8 triangles = 408 bytes.
 

Here, ther eseems to be very few difference, but we do not share UVs (note if all the "1" were on the same side we could have used tiling and get the same count as previous example).

So vertices are split in our 3d engine. So we have a vertex for each red dot.
16 vertices + 8 triangles = 688 bytes (+70%)


In this case, I explain with normals. Here I have "soft edges" (smooth groups), so normals in the middle of the objet are the same for each side. You count 9 positions & 9 normals so there will be 9 vertices.
9 vertices + 8 triangles = 408 bytes.


If I put "hard edges", there are 2 normals for the points in the middle, so points will each one give 2 vertices. We'll have 12 vertices.
12 vertices + 8 triangles = 528 bytes (+30%)


Same for color-per-vertex, smooth color:
9 vertices + 8 triangles = 408 bytes.


Hard color change : 12 vertices.
12 vertices + 8 triangles = 528 bytes (+30%)


So what ?
Just keep this in mind.
- An "hard edge" model can be twice the weight of a "smooth" one.
- An "automatic layout" of UVs can be twice the weight of a "planar" UV mapping
- Most of the split in game models are due to UVs (UV1 or UV2)

Any advice ?
- Remove any unused UV layer (your vertices could be split because of an unused UV2 layer)
- If some of the Uvs of your model map to a single flat color, try to put all UVs in a single Uv position better than mapping it on a quad on the flat surface (and it's wasted place in the texture :)
- Smooth models. Only hard edge some small parts, if you really need to.
- You can simulate some hard edges with normal map

Well, lots of other things ! Just tell me in the comments if you have other ideas :)

Remarks:
- Same thing occurs with material change (with even more overhead due to other factors)

- Understand that if a point is already split by UVs for example (like in picture 2), having hard normals or hard colors won't split it again. In fact having 16 vertices for this mesh is the worst case.


- This leads to some optimizations like double edges, that I'll try to cover one day



References/Extra links
  • http://www.polycount.com/forum/showthread.php?t=50588
  • http://www.ericchadwick.com/examples/provost/byf1.html 

No comments:

Post a Comment