Skip to content

Commit c313aba

Browse files
authored
Merge pull request #1103 from zeux/perftune
Assorted performance improvements
2 parents 7d0e9df + 840047b commit c313aba

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎src/clusterizer.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,12 @@ static size_t kdtreeBuild(size_t offset, KDNode* nodes, size_t node_count, const
560560
float split = mean[axis];
561561
size_t middle = kdtreePartition(indices, count, points, stride, axis, split);
562562

563-
// when the partition is degenerate simply consolidate the points into a single node
564-
// this also ensures recursion depth is bounded on pathological inputs
565-
if (middle <= leaf_size / 2 || middle >= count - leaf_size / 2 || depth >= kMeshletMaxTreeDepth)
563+
// when the partition is degenerate, consolidate the points into a single node; also ensure recursion depth is bounded on pathological inputs
564+
if (middle == 0 || middle == count || depth >= kMeshletMaxTreeDepth)
565+
return kdtreeBuildLeaf(offset, nodes, node_count, indices, count);
566+
567+
// when the partition is unbalanced, consolidate the points into a single small node; large nodes are still split to prevent outlier points from creating very large leaves
568+
if ((middle <= leaf_size / 2 || middle >= count - leaf_size / 2) && count <= leaf_size * 8)
566569
return kdtreeBuildLeaf(offset, nodes, node_count, indices, count);
567570

568571
KDNode& result = nodes[offset];

‎src/indexcodec.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ static int rotateTriangle(unsigned int a, unsigned int b, unsigned int c, unsign
3333

3434
static int getEdgeFifo(EdgeFifo fifo, unsigned int a, unsigned int b, unsigned int c, size_t offset)
3535
{
36+
unsigned long long ab = ((unsigned long long)a << 32) | b;
37+
unsigned long long bc = ((unsigned long long)b << 32) | c;
38+
unsigned long long ca = ((unsigned long long)c << 32) | a;
39+
3640
for (int i = 0; i < 16; ++i)
3741
{
3842
size_t index = (offset - 1 - i) & 15;
3943

4044
unsigned int e0 = fifo[index][0];
4145
unsigned int e1 = fifo[index][1];
46+
unsigned long long e = ((unsigned long long)e0 << 32) | e1;
4247

43-
if (e0 == a && e1 == b)
48+
if (e == ab)
4449
return (i << 2) | 0;
45-
if (e0 == b && e1 == c)
50+
if (e == bc)
4651
return (i << 2) | 1;
47-
if (e0 == c && e1 == a)
52+
if (e == ca)
4853
return (i << 2) | 2;
4954
}
5055

‎src/meshletutils.cpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const size_t kMeshletMaxVertices = 256;
1919
// A reasonable limit is around 2*max_vertices or less
2020
const size_t kMeshletMaxTriangles = 512;
2121

22-
static void computeBoundingSphere(float result[4], const float* points, size_t count, size_t points_stride, const float* radii, size_t radii_stride, size_t axis_count, const unsigned int* indices = NULL)
22+
template <int Axes>
23+
static void computeBoundingSphere(float result[4], const float* points, size_t count, size_t points_stride, const float* radii, size_t radii_stride, const unsigned int* indices = NULL)
2324
{
2425
static const float axes[7][3] = {
2526
// X, Y, Z
@@ -35,7 +36,7 @@ static void computeBoundingSphere(float result[4], const float* points, size_t c
3536
};
3637

3738
assert(count > 0);
38-
assert(axis_count <= sizeof(axes) / sizeof(axes[0]));
39+
assert(size_t(Axes) <= sizeof(axes) / sizeof(axes[0]));
3940

4041
size_t points_stride_float = points_stride / sizeof(float);
4142
size_t radii_stride_float = radii_stride / sizeof(float);
@@ -44,7 +45,7 @@ static void computeBoundingSphere(float result[4], const float* points, size_t c
4445
unsigned int pmin[7], pmax[7];
4546
float tmin[7], tmax[7];
4647

47-
for (size_t axis = 0; axis < axis_count; ++axis)
48+
for (int axis = 0; axis < Axes; ++axis)
4849
{
4950
pmin[axis] = pmax[axis] = 0;
5051
tmin[axis] = FLT_MAX;
@@ -57,7 +58,7 @@ static void computeBoundingSphere(float result[4], const float* points, size_t c
5758
const float* p = points + v * points_stride_float;
5859
float r = radii[v * radii_stride_float];
5960

60-
for (size_t axis = 0; axis < axis_count; ++axis)
61+
for (int axis = 0; axis < Axes; ++axis)
6162
{
6263
const float* ax = axes[axis];
6364

@@ -75,7 +76,7 @@ static void computeBoundingSphere(float result[4], const float* points, size_t c
7576
size_t paxis = 0;
7677
float paxisdr = 0;
7778

78-
for (size_t axis = 0; axis < axis_count; ++axis)
79+
for (int axis = 0; axis < Axes; ++axis)
7980
{
8081
const float* p1 = points + pmin[axis] * points_stride_float;
8182
const float* p2 = points + pmax[axis] * points_stride_float;
@@ -182,13 +183,13 @@ static meshopt_Bounds computeClusterBounds(const unsigned int* indices, size_t i
182183

183184
// compute cluster bounding sphere; we'll use the center to determine normal cone apex as well
184185
float psphere[4] = {};
185-
computeBoundingSphere(psphere, vertex_positions, corner_count, vertex_positions_stride, &rzero, 0, 7, corners);
186+
computeBoundingSphere<7>(psphere, vertex_positions, corner_count, vertex_positions_stride, &rzero, 0, corners);
186187

187188
float center[3] = {psphere[0], psphere[1], psphere[2]};
188189

189190
// treating triangle normals as points, find the bounding sphere - the sphere center determines the optimal cone axis
190191
float nsphere[4] = {};
191-
computeBoundingSphere(nsphere, normals[0], triangles, sizeof(float) * 4, &rzero, 0, 3);
192+
computeBoundingSphere<3>(nsphere, normals[0], triangles, sizeof(float) * 4, &rzero, 0);
192193

193194
float axis[3] = {nsphere[0], nsphere[1], nsphere[2]};
194195
float axislength = sqrtf(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
@@ -353,7 +354,7 @@ meshopt_Bounds meshopt_computeSphereBounds(const float* positions, size_t count,
353354
const float rzero = 0.f;
354355

355356
float psphere[4] = {};
356-
computeBoundingSphere(psphere, positions, count, positions_stride, radii ? radii : &rzero, radii ? radii_stride : 0, 7);
357+
computeBoundingSphere<7>(psphere, positions, count, positions_stride, radii ? radii : &rzero, radii ? radii_stride : 0);
357358

358359
bounds.center[0] = psphere[0];
359360
bounds.center[1] = psphere[1];

0 commit comments

Comments
 (0)