Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AHashMap for SurfaceTool #101273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions scene/resources/surface_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "surface_tool.h"

#include "core/templates/a_hash_map.h"

#define EQ_VERTEX_DIST 0.00001

SurfaceTool::OptimizeVertexCacheFunc SurfaceTool::optimize_vertex_cache_func = nullptr;
Expand All @@ -51,7 +53,7 @@ void SurfaceTool::strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32A
r_vertices.resize(new_vertex_count);
remap_index_func((unsigned int *)r_indices.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), remap.ptr());

HashMap<const int *, bool, TriangleHasher, TriangleHasher> found_triangles;
AHashMap<const int *, bool, TriangleHasher, TriangleHasher> found_triangles;
int *idx_ptr = r_indices.ptrw();

int filtered_indices_count = 0;
Expand All @@ -70,7 +72,7 @@ void SurfaceTool::strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32A
memcpy(idx_ptr + (filtered_indices_count * 3), tri, sizeof(int) * 3);
}

found_triangles[tri] = true;
found_triangles.insert_new(tri, true);
filtered_indices_count++;
}
r_indices.resize(filtered_indices_count * 3);
Expand Down Expand Up @@ -135,18 +137,12 @@ bool SurfaceTool::Vertex::operator==(const Vertex &p_vertex) const {
}

uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h);
h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
uint32_t h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int));
h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.custom[0], sizeof(Color) * RS::ARRAY_CUSTOM_COUNT, h);
h = hash_murmur3_one_32(p_vtx.smooth_group, h);
h = hash_fmix32(h);

const int64_t length = (int64_t)&p_vtx.vertex - (int64_t)&p_vtx.smooth_group + sizeof(p_vtx.vertex);
const void *key = &p_vtx.smooth_group;
h = hash_murmur3_buffer(key, length, h);
return h;
}

Expand All @@ -163,7 +159,7 @@ bool SurfaceTool::SmoothGroupVertex::operator==(const SmoothGroupVertex &p_verte
}

uint32_t SurfaceTool::SmoothGroupVertexHasher::hash(const SmoothGroupVertex &p_vtx) {
uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
uint32_t h = HashMapHasherDefault::hash(p_vtx.vertex);
h = hash_murmur3_one_32(p_vtx.smooth_group, h);
h = hash_fmix32(h);
return h;
Expand Down Expand Up @@ -755,24 +751,25 @@ void SurfaceTool::index() {
return; //already indexed
}

HashMap<Vertex, int, VertexHasher> indices;
LocalVector<Vertex> old_vertex_array = vertex_array;
vertex_array.clear();
AHashMap<Vertex &, int, VertexHasher> indices = vertex_array.size();

for (const Vertex &vertex : old_vertex_array) {
uint32_t new_size = 0;
for (Vertex &vertex : vertex_array) {
int *idxptr = indices.getptr(vertex);
int idx;
if (!idxptr) {
idx = indices.size();
vertex_array.push_back(vertex);
indices[vertex] = idx;
vertex_array[new_size] = vertex;
indices.insert_new(vertex_array[new_size], idx);
new_size++;
} else {
idx = *idxptr;
}

index_array.push_back(idx);
}

vertex_array.resize(new_size);
format |= Mesh::ARRAY_FORMAT_INDEX;
}

Expand Down Expand Up @@ -1197,7 +1194,7 @@ void SurfaceTool::generate_normals(bool p_flip) {

ERR_FAIL_COND((vertex_array.size() % 3) != 0);

HashMap<SmoothGroupVertex, Vector3, SmoothGroupVertexHasher> smooth_hash;
AHashMap<SmoothGroupVertex, Vector3, SmoothGroupVertexHasher> smooth_hash = vertex_array.size();

for (uint32_t vi = 0; vi < vertex_array.size(); vi += 3) {
Vertex *v = &vertex_array[vi];
Expand All @@ -1214,7 +1211,7 @@ void SurfaceTool::generate_normals(bool p_flip) {
if (v[i].smooth_group != UINT32_MAX) {
Vector3 *lv = smooth_hash.getptr(v[i]);
if (!lv) {
smooth_hash.insert(v[i], normal);
smooth_hash.insert_new(v[i], normal);
} else {
(*lv) += normal;
}
Expand Down
14 changes: 10 additions & 4 deletions scene/resources/surface_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,23 @@ class SurfaceTool : public RefCounted {

public:
struct Vertex {
Vector3 vertex;
// Trivial data for which the hash is computed using hash_buffer.
// ----------------------------------------------------------------
uint32_t smooth_group = 0; // Must be first.

Color color;
Vector3 normal; // normal, binormal, tangent
Vector3 normal; // normal, binormal, tangent.
Vector3 binormal;
Vector3 tangent;
Vector2 uv;
Vector2 uv2;
Color custom[RS::ARRAY_CUSTOM_COUNT];

Vector3 vertex; // Must be last.
// ----------------------------------------------------------------

Vector<int> bones;
Vector<float> weights;
Color custom[RS::ARRAY_CUSTOM_COUNT];
uint32_t smooth_group = 0;

bool operator==(const Vertex &p_vertex) const;

Expand Down