Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/meshoptimizer/spatialorder.cpp
9903 views
1
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
2
#include "meshoptimizer.h"
3
4
#include <assert.h>
5
#include <float.h>
6
#include <string.h>
7
8
// This work is based on:
9
// Fabian Giesen. Decoding Morton codes. 2009
10
namespace meshopt
11
{
12
13
// "Insert" two 0 bits after each of the 20 low bits of x
14
inline unsigned long long part1By2(unsigned long long x)
15
{
16
x &= 0x000fffffull; // x = ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- jihg fedc ba98 7654 3210
17
x = (x ^ (x << 32)) & 0x000f00000000ffffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- ---- ---- ---- ---- fedc ba98 7654 3210
18
x = (x ^ (x << 16)) & 0x000f0000ff0000ffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- fedc ba98 ---- ---- ---- ---- 7654 3210
19
x = (x ^ (x << 8)) & 0x000f00f00f00f00full; // x = ---- ---- ---- jihg ---- ---- fedc ---- ---- ba98 ---- ---- 7654 ---- ---- 3210
20
x = (x ^ (x << 4)) & 0x00c30c30c30c30c3ull; // x = ---- ---- ji-- --hg ---- fe-- --dc ---- ba-- --98 ---- 76-- --54 ---- 32-- --10
21
x = (x ^ (x << 2)) & 0x0249249249249249ull; // x = ---- --j- -i-- h--g --f- -e-- d--c --b- -a-- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
22
return x;
23
}
24
25
static void computeOrder(unsigned long long* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, bool morton)
26
{
27
size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
28
29
float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
30
float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
31
32
for (size_t i = 0; i < vertex_count; ++i)
33
{
34
const float* v = vertex_positions_data + i * vertex_stride_float;
35
36
for (int j = 0; j < 3; ++j)
37
{
38
float vj = v[j];
39
40
minv[j] = minv[j] > vj ? vj : minv[j];
41
maxv[j] = maxv[j] < vj ? vj : maxv[j];
42
}
43
}
44
45
float extent = 0.f;
46
47
extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
48
extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
49
extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
50
51
// rescale each axis to 16 bits to get 48-bit Morton codes
52
float scale = extent == 0 ? 0.f : 65535.f / extent;
53
54
// generate Morton order based on the position inside a unit cube
55
for (size_t i = 0; i < vertex_count; ++i)
56
{
57
const float* v = vertex_positions_data + i * vertex_stride_float;
58
59
int x = int((v[0] - minv[0]) * scale + 0.5f);
60
int y = int((v[1] - minv[1]) * scale + 0.5f);
61
int z = int((v[2] - minv[2]) * scale + 0.5f);
62
63
if (morton)
64
result[i] = part1By2(x) | (part1By2(y) << 1) | (part1By2(z) << 2);
65
else
66
result[i] = ((unsigned long long)x << 0) | ((unsigned long long)y << 20) | ((unsigned long long)z << 40);
67
}
68
}
69
70
static void radixSort10(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count)
71
{
72
unsigned int hist[1024];
73
memset(hist, 0, sizeof(hist));
74
75
// compute histogram (assume keys are 10-bit)
76
for (size_t i = 0; i < count; ++i)
77
hist[keys[i]]++;
78
79
unsigned int sum = 0;
80
81
// replace histogram data with prefix histogram sums in-place
82
for (int i = 0; i < 1024; ++i)
83
{
84
unsigned int h = hist[i];
85
hist[i] = sum;
86
sum += h;
87
}
88
89
assert(sum == count);
90
91
// reorder values
92
for (size_t i = 0; i < count; ++i)
93
{
94
unsigned int id = keys[source[i]];
95
96
destination[hist[id]++] = source[i];
97
}
98
}
99
100
static void computeHistogram(unsigned int (&hist)[256][2], const unsigned short* data, size_t count)
101
{
102
memset(hist, 0, sizeof(hist));
103
104
// compute 2 8-bit histograms in parallel
105
for (size_t i = 0; i < count; ++i)
106
{
107
unsigned long long id = data[i];
108
109
hist[(id >> 0) & 255][0]++;
110
hist[(id >> 8) & 255][1]++;
111
}
112
113
unsigned int sum0 = 0, sum1 = 0;
114
115
// replace histogram data with prefix histogram sums in-place
116
for (int i = 0; i < 256; ++i)
117
{
118
unsigned int h0 = hist[i][0], h1 = hist[i][1];
119
120
hist[i][0] = sum0;
121
hist[i][1] = sum1;
122
123
sum0 += h0;
124
sum1 += h1;
125
}
126
127
assert(sum0 == count && sum1 == count);
128
}
129
130
static void radixPass(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count, unsigned int (&hist)[256][2], int pass)
131
{
132
int bitoff = pass * 8;
133
134
for (size_t i = 0; i < count; ++i)
135
{
136
unsigned int id = unsigned(keys[source[i]] >> bitoff) & 255;
137
138
destination[hist[id][pass]++] = source[i];
139
}
140
}
141
142
static void partitionPoints(unsigned int* target, const unsigned int* order, const unsigned char* sides, size_t split, size_t count)
143
{
144
size_t l = 0, r = split;
145
146
for (size_t i = 0; i < count; ++i)
147
{
148
unsigned char side = sides[order[i]];
149
target[side ? r : l] = order[i];
150
l += 1;
151
l -= side;
152
r += side;
153
}
154
155
assert(l == split && r == count);
156
}
157
158
static void splitPoints(unsigned int* destination, unsigned int* orderx, unsigned int* ordery, unsigned int* orderz, const unsigned long long* keys, size_t count, void* scratch, size_t cluster_size)
159
{
160
if (count <= cluster_size)
161
{
162
memcpy(destination, orderx, count * sizeof(unsigned int));
163
return;
164
}
165
166
unsigned int* axes[3] = {orderx, ordery, orderz};
167
168
int bestk = -1;
169
unsigned int bestdim = 0;
170
171
for (int k = 0; k < 3; ++k)
172
{
173
const unsigned int mask = (1 << 20) - 1;
174
unsigned int dim = (unsigned(keys[axes[k][count - 1]] >> (k * 20)) & mask) - (unsigned(keys[axes[k][0]] >> (k * 20)) & mask);
175
176
if (dim >= bestdim)
177
{
178
bestk = k;
179
bestdim = dim;
180
}
181
}
182
183
assert(bestk >= 0);
184
185
// split roughly in half, with the left split always being aligned to cluster size
186
size_t split = ((count / 2) + cluster_size - 1) / cluster_size * cluster_size;
187
assert(split > 0 && split < count);
188
189
// mark sides of split for partitioning
190
unsigned char* sides = static_cast<unsigned char*>(scratch) + count * sizeof(unsigned int);
191
192
for (size_t i = 0; i < split; ++i)
193
sides[axes[bestk][i]] = 0;
194
195
for (size_t i = split; i < count; ++i)
196
sides[axes[bestk][i]] = 1;
197
198
// partition all axes into two sides, maintaining order
199
unsigned int* temp = static_cast<unsigned int*>(scratch);
200
201
for (int k = 0; k < 3; ++k)
202
{
203
if (k == bestk)
204
continue;
205
206
unsigned int* axis = axes[k];
207
memcpy(temp, axis, sizeof(unsigned int) * count);
208
partitionPoints(axis, temp, sides, split, count);
209
}
210
211
splitPoints(destination, orderx, ordery, orderz, keys, split, scratch, cluster_size);
212
splitPoints(destination + split, orderx + split, ordery + split, orderz + split, keys, count - split, scratch, cluster_size);
213
}
214
215
} // namespace meshopt
216
217
void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
218
{
219
using namespace meshopt;
220
221
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
222
assert(vertex_positions_stride % sizeof(float) == 0);
223
224
meshopt_Allocator allocator;
225
226
unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);
227
computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ true);
228
229
unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 2b for keys
230
unsigned short* keyk = (unsigned short*)(scratch + vertex_count);
231
232
for (size_t i = 0; i < vertex_count; ++i)
233
destination[i] = unsigned(i);
234
235
unsigned int* order[] = {scratch, destination};
236
237
// 5-pass radix sort computes the resulting order into scratch
238
for (int k = 0; k < 5; ++k)
239
{
240
// copy 10-bit key segments into keyk to reduce cache pressure during radix pass
241
for (size_t i = 0; i < vertex_count; ++i)
242
keyk[i] = (unsigned short)((keys[i] >> (k * 10)) & 1023);
243
244
radixSort10(order[k % 2], order[(k + 1) % 2], keyk, vertex_count);
245
}
246
247
// since our remap table is mapping old=>new, we need to reverse it
248
for (size_t i = 0; i < vertex_count; ++i)
249
destination[scratch[i]] = unsigned(i);
250
}
251
252
void meshopt_spatialSortTriangles(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
253
{
254
using namespace meshopt;
255
256
assert(index_count % 3 == 0);
257
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
258
assert(vertex_positions_stride % sizeof(float) == 0);
259
260
(void)vertex_count;
261
262
size_t face_count = index_count / 3;
263
size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
264
265
meshopt_Allocator allocator;
266
267
float* centroids = allocator.allocate<float>(face_count * 3);
268
269
for (size_t i = 0; i < face_count; ++i)
270
{
271
unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
272
assert(a < vertex_count && b < vertex_count && c < vertex_count);
273
274
const float* va = vertex_positions + a * vertex_stride_float;
275
const float* vb = vertex_positions + b * vertex_stride_float;
276
const float* vc = vertex_positions + c * vertex_stride_float;
277
278
centroids[i * 3 + 0] = (va[0] + vb[0] + vc[0]) / 3.f;
279
centroids[i * 3 + 1] = (va[1] + vb[1] + vc[1]) / 3.f;
280
centroids[i * 3 + 2] = (va[2] + vb[2] + vc[2]) / 3.f;
281
}
282
283
unsigned int* remap = allocator.allocate<unsigned int>(face_count);
284
285
meshopt_spatialSortRemap(remap, centroids, face_count, sizeof(float) * 3);
286
287
// support in-order remap
288
if (destination == indices)
289
{
290
unsigned int* indices_copy = allocator.allocate<unsigned int>(index_count);
291
memcpy(indices_copy, indices, index_count * sizeof(unsigned int));
292
indices = indices_copy;
293
}
294
295
for (size_t i = 0; i < face_count; ++i)
296
{
297
unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
298
unsigned int r = remap[i];
299
300
destination[r * 3 + 0] = a;
301
destination[r * 3 + 1] = b;
302
destination[r * 3 + 2] = c;
303
}
304
}
305
306
void meshopt_spatialClusterPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t cluster_size)
307
{
308
using namespace meshopt;
309
310
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
311
assert(vertex_positions_stride % sizeof(float) == 0);
312
assert(cluster_size > 0);
313
314
meshopt_Allocator allocator;
315
316
unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);
317
computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ false);
318
319
unsigned int* order = allocator.allocate<unsigned int>(vertex_count * 3);
320
unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 1b for side or 2b for keys
321
unsigned short* keyk = reinterpret_cast<unsigned short*>(scratch + vertex_count);
322
323
for (int k = 0; k < 3; ++k)
324
{
325
// copy 16-bit key segments into keyk to reduce cache pressure during radix pass
326
for (size_t i = 0; i < vertex_count; ++i)
327
keyk[i] = (unsigned short)(keys[i] >> (k * 20));
328
329
unsigned int hist[256][2];
330
computeHistogram(hist, keyk, vertex_count);
331
332
for (size_t i = 0; i < vertex_count; ++i)
333
order[k * vertex_count + i] = unsigned(i);
334
335
radixPass(scratch, order + k * vertex_count, keyk, vertex_count, hist, 0);
336
radixPass(order + k * vertex_count, scratch, keyk, vertex_count, hist, 1);
337
}
338
339
splitPoints(destination, order, order + vertex_count, order + 2 * vertex_count, keys, vertex_count, scratch, cluster_size);
340
}
341
342