·5 min read

Smaller GLB files: 16-bit indices, no normals

Three lossless changes to our glTF export: 16-bit indices, vertex cache reordering and position-only collision meshes. What we measured on 36 files, and what we did not.

gamedevprocedural-generationarchitecture

Contents

We measured all 36 GLB files our asset builder had produced, 733,000 triangles and 807,000 vertices, and found three free wins in how we wrote them. None changes how a model looks. This post lists what each one is, the numbers behind it, and which claims we did not measure.

What we measured first

Before changing the exporter, we counted. Across the 36 files:

  • Every index buffer used 32-bit indices, even though every mesh had fewer than 65,535 vertices. That is 4.4 MB of index data where about half would do.
  • Triangles were stored in the order the generator made them. A GPU runs the vertex shader again for each vertex it has not seen recently, and our order caused 1.55 cache misses per triangle. After reordering it was 1.13. That is about 37 percent more vertex shader runs than needed.
  • Collision meshes carried normals, and vertices split by those normals. Physics never reads normals. On the tank, the collision mesh had 22,900 vertices where 14,200 were enough.
  • Duplicate or unused vertices: almost none. The weld step we already had was working. We checked, because it was the first guess, and it was not the problem.

One: 16-bit indices

An index says which vertex a triangle corner uses. With 32 bits, a mesh can have four billion vertices. With 16 bits, 65,535. Our meshes were far below that, so every index spent two bytes on nothing.

glTF allows this directly: the accessor's component type can be UNSIGNED_SHORT (5123) instead of UNSIGNED_INT (5125). The rule we use is plain: if a mesh has fewer than 65,535 vertices, write 16-bit indices, else 32-bit. Index 65,535 is left out because it is a reserved restart value in some graphics APIs. Every mesh we had qualified.

Two: reorder for the vertex cache

A GPU keeps a small cache of recently transformed vertices. If the next triangle uses vertices still in the cache, they cost nothing. If not, the vertex shader runs again. The order of triangles decides how often that happens, and the same triangles in a better order give the same picture for less work.

Reordering does not change the triangles, their winding, or any vertex value. It changes only the order of the index list and, as a second step, the order of the vertex buffer so that vertices are read in the order they are used. A library such as meshoptimizer has this as a ready function, and our exporter uses an optimiser of that kind. We checked on real meshes and with a new test that the set of triangles before and after is the same.

On one rebuilt model, the jerry can, the misses per triangle went from 1.34 to 1.00. The whole-set figure above is 1.55 to 1.13.

Three: collision meshes with positions only

A collision mesh is a stand-in used by physics. It needs where the surface is, not how it is shaded. We were writing normals into it, and because hard edges split vertices to hold different normals, we also wrote extra vertices.

Now collision meshes are positions only, with vertices merged by position. On the jerry can, the collision mesh went from 255 vertices to 197. On the tank, from 22,900 to 14,200.

One shared function

All three live in one function that both the glTF and the USD exporters call. That matters more than it sounds. If each exporter did its own finishing, a fix would have to be made twice and the two files could differ in subtle ways. The new test checks that triangles and winding survive, and that vertex values are the same.

We bumped the exporter version too, so each asset re-exports on its next build and old files do not quietly stay in use.

What we checked, and what we did not

  • A real rebuild of the jerry can was accepted, with all 102 of its checks passing. The new test and the existing export tests passed: 10 passed, 1 skipped.
  • The jerry can file got bigger, from 222 KB to 422 KB. That was not caused by this change. The old file had no textures, and the new build added baked normal and occlusion maps, which come with extra vertex data. Do not read the size as a regression, and do not read it as a win either.
  • We did not rebuild the other 35 files. Their numbers are projections from the measurements above, not results.
  • There was no stored speed baseline for the jerry can, so the required speed check did not produce a comparison. Export itself took 0.4 s of a 13.4 s build.

What we left alone

The biggest remaining cost in vertex count is hard-edge normal splits. On the tank, 394,000 vertices sit on only 177,000 distinct positions, so the splits roughly double the count. Cutting that is not lossless: it softens shading, or needs engine support.

Options we listed and did not take:

  • KHR_mesh_quantization, about 40 to 50 percent smaller vertex data with sub-millimetre position error.
  • EXT_meshopt_compression, lossless in itself, but your engine must read the extension.
  • Merging more normals on far LODs, which gives fewer vertices and slightly softer shading at a distance.

Each of those changes what a player sees or what the engine must support, so they are a choice, not a free win. The three above are the ones you can ship without asking anyone.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.