Skip to content

Commit

Permalink
gltfpack: Use strip-optimized index encoding
Browse files Browse the repository at this point in the history
This makes index buffer size more or less the same before zlib, but
noticeably smaller after zlib, and additionally improves vertex
compression ratio before and after zlib, sometimes dramatically.

Because the resulting index order is less efficient from ACMR
perspective, this is only done when high compression option (-cc) is
chosen.
  • Loading branch information
zeux committed Jan 27, 2020
1 parent 3b4d1c9 commit b4478e2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tools/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct Settings
int texture_quality;

bool compress;
bool compressmore;
bool fallback;

int verbose;
Expand Down Expand Up @@ -1151,11 +1152,14 @@ void simplifyMesh(Mesh& mesh, float threshold, bool aggressive)
}
}

void optimizeMesh(Mesh& mesh)
void optimizeMesh(Mesh& mesh, bool compressmore)
{
size_t vertex_count = mesh.streams[0].data.size();

meshopt_optimizeVertexCache(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), vertex_count);
if (compressmore)
meshopt_optimizeVertexCacheStrip(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), vertex_count);
else
meshopt_optimizeVertexCache(&mesh.indices[0], &mesh.indices[0], mesh.indices.size(), vertex_count);

std::vector<unsigned int> remap(vertex_count);
size_t unique_vertices = meshopt_optimizeVertexFetchRemap(&remap[0], &mesh.indices[0], mesh.indices.size(), vertex_count);
Expand Down Expand Up @@ -1349,7 +1353,7 @@ void processMesh(Mesh& mesh, const Settings& settings)
reindexMesh(mesh);
filterTriangles(mesh);
simplifyMesh(mesh, settings.simplify_threshold, settings.simplify_aggressive);
optimizeMesh(mesh);
optimizeMesh(mesh, settings.compressmore);
break;

default:
Expand Down Expand Up @@ -4598,6 +4602,11 @@ int main(int argc, char** argv)
{
settings.compress = true;
}
else if (strcmp(arg, "-cc") == 0)
{
settings.compress = true;
settings.compressmore = true;
}
else if (strcmp(arg, "-cf") == 0)
{
settings.compress = true;
Expand Down Expand Up @@ -4667,6 +4676,7 @@ int main(int argc, char** argv)
fprintf(stderr, "-tc: convert all textures to KTX2 with BasisU supercompression (using basisu executable)\n");
fprintf(stderr, "-tq N: set texture encoding quality (default: 50; N should be between 1 and 100\n");
fprintf(stderr, "-c: produce compressed gltf/glb files\n");
fprintf(stderr, "-cc: produce compressed gltf/glb files with higher compression ratio\n");
fprintf(stderr, "-cf: produce compressed gltf/glb files with fallback for loaders that don't support compression\n");
fprintf(stderr, "-v: verbose output (print version when used without other options)\n");
fprintf(stderr, "-h: display this help and exit\n");
Expand Down

0 comments on commit b4478e2

Please sign in to comment.