Skip to content

Commit

Permalink
Merge pull request #790 from zeux/gltf-ddperf
Browse files Browse the repository at this point in the history
gltfpack: Accelerate deduplication pass for scenes with a lot of primitives
  • Loading branch information
zeux authored Oct 16, 2024
2 parents 7f936ad + 7a88aff commit 8c86684
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 14 additions & 2 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "gltfpack.h"

#include <algorithm>
#include <map>
#include <unordered_map>

#include <locale.h>
#include <stdint.h>
Expand Down Expand Up @@ -329,6 +329,18 @@ static bool isExtensionSupported(const ExtensionInfo* extensions, size_t count,
return false;
}

namespace std
{
template <>
struct hash<std::pair<uint64_t, uint64_t> >
{
size_t operator()(const std::pair<uint64_t, uint64_t>& x) const
{
return std::hash<uint64_t>()(x.first ^ x.second);
}
};
} // namespace std

static void process(cgltf_data* data, const char* input_path, const char* output_path, const char* report_path, std::vector<Mesh>& meshes, std::vector<Animation>& animations, const Settings& settings, std::string& json, std::string& bin, std::string& fallback, size_t& fallback_size)
{
if (settings.verbose)
Expand Down Expand Up @@ -565,7 +577,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output
ext_texture_transform = ext_texture_transform || mi.uses_texture_transform;
}

std::map<std::pair<uint64_t, uint64_t>, std::pair<size_t, size_t> > primitive_cache;
std::unordered_map<std::pair<uint64_t, uint64_t>, std::pair<size_t, size_t> > primitive_cache;

for (size_t i = 0; i < meshes.size(); ++i)
{
Expand Down
25 changes: 23 additions & 2 deletions gltf/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "gltfpack.h"

#include <algorithm>
#include <unordered_map>

#include <math.h>
#include <stdint.h>
Expand Down Expand Up @@ -355,8 +356,15 @@ static bool canDedupMesh(const Mesh& mesh)

void dedupMeshes(std::vector<Mesh>& meshes)
{
std::unordered_map<uint64_t, int> hashes;

for (size_t i = 0; i < meshes.size(); ++i)
hashMesh(meshes[i]);
{
Mesh& mesh = meshes[i];

hashMesh(mesh);
hashes[mesh.geometry_hash[0] ^ mesh.geometry_hash[1]]++;
}

for (size_t i = 0; i < meshes.size(); ++i)
{
Expand All @@ -365,6 +373,9 @@ void dedupMeshes(std::vector<Mesh>& meshes)
if (!canDedupMesh(target))
continue;

if (hashes[target.geometry_hash[0] ^ target.geometry_hash[1]] <= 1)
continue;

for (size_t j = i + 1; j < meshes.size(); ++j)
{
Mesh& mesh = meshes[j];
Expand Down Expand Up @@ -397,6 +408,16 @@ void dedupMeshes(std::vector<Mesh>& meshes)
mesh.instances.clear();
}
}

for (size_t i = 0; i < meshes.size(); ++i)
{
Mesh& target = meshes[i];
if (target.nodes.size() <= 1)
continue;

std::sort(target.nodes.begin(), target.nodes.end());
target.nodes.erase(std::unique(target.nodes.begin(), target.nodes.end()), target.nodes.end());
}
}

void mergeMeshInstances(Mesh& mesh)
Expand Down Expand Up @@ -759,7 +780,7 @@ static void simplifyMesh(Mesh& mesh, float threshold, float error, bool attribut

size_t vertex_count = mesh.streams[0].data.size();

size_t target_index_count = size_t(double(mesh.indices.size() / 3) * threshold) * 3;
size_t target_index_count = size_t(double(size_t(mesh.indices.size() / 3)) * threshold) * 3;
float target_error = error;
float target_error_aggressive = 1e-1f;

Expand Down

0 comments on commit 8c86684

Please sign in to comment.