forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
374 lines (313 loc) · 10.9 KB
/
mesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <math.h> // powf
#include <assert.h>
#include "mesh.h"
#include "engine.h"
#include "u_file.h"
#include "u_map.h"
///! vertexCacheData
size_t vertexCacheData::findTriangle(size_t triangle) {
for (size_t i = 0; i < indices.size(); i++)
if (indices[i] == triangle)
return i;
return (size_t)-1;
}
void vertexCacheData::moveTriangle(size_t triangle) {
size_t index = findTriangle(triangle);
assert(index != (size_t)-1);
// Erase the index and add it to the end
indices.erase(indices.begin() + index,
indices.begin() + index + 1);
indices.push_back(triangle);
}
vertexCacheData::vertexCacheData()
: cachePosition((size_t)-1)
, currentScore(0.0f)
, totalValence(0)
, remainingValence(0)
, calculated(false)
{
}
///!triangleCacheData
triangleCacheData::triangleCacheData()
: rendered(false)
, currentScore(0.0f)
, calculated(false)
{
vertices[0] = (size_t)-1;
vertices[1] = (size_t)-1;
vertices[2] = (size_t)-1;
}
///! vertexCache
size_t vertexCache::findVertex(size_t vertex) {
for (size_t i = 0; i < 32; i++)
if (m_cache[i] == vertex)
return i;
return (size_t)-1;
}
void vertexCache::removeVertex(size_t stackIndex) {
for (size_t i = stackIndex; i < 38; i++)
m_cache[i] = m_cache[i + 1];
}
void vertexCache::addVertex(size_t vertex) {
size_t w = findVertex(vertex);
// remove the vertex for later reinsertion at the top
if (w != (size_t)-1)
removeVertex(w);
else // not found, cache miss!
m_misses++;
// shift all vertices down to make room for the new top vertex
for (size_t i = 39; i != 0; i--)
m_cache[i] = m_cache[i - 1];
// add new vertex to cache
m_cache[0] = vertex;
}
void vertexCache::clear() {
for (size_t i = 0; i < 40; i++)
m_cache[i] = (size_t)-1;
m_misses = 0;
}
size_t vertexCache::getCacheMissCount() const {
return m_misses;
}
size_t vertexCache::getCacheMissCount(const u::vector<size_t> &indices) {
clear();
for (auto &it : indices)
addVertex(it);
return m_misses;
}
size_t vertexCache::getCachedVertex(size_t index) const {
return m_cache[index];
}
vertexCache::vertexCache() {
clear();
}
///! vertexCacheOptimizer
static constexpr float kCacheDecayPower = 1.5f;
static constexpr float kLastTriScore = 0.75f;
static constexpr float kValenceBoostScale = 2.0f;
static constexpr float kValenceBoostPower = 0.5f;
vertexCacheOptimizer::vertexCacheOptimizer()
: m_bestTriangle(0)
{
}
vertexCacheOptimizer::result vertexCacheOptimizer::optimize(u::vector<size_t> &indices) {
size_t find = (size_t)-1;
for (size_t i = 0; i < indices.size(); i++)
if (find == (size_t)-1 || (find != (size_t)-1 && indices[i] > find))
find = indices[i];
if (find == (size_t)-1)
return kErrorNoVertices;
result begin = init(indices, find + 1);
if (begin != kSuccess)
return begin;
// Process
while (process())
;
// Rewrite the indices
for (size_t i = 0; i < m_drawList.size(); i++)
for (size_t j = 0; j < 3; j++)
indices[3 * i + j] = m_triangles[m_drawList[i]].vertices[j];
return kSuccess;
}
float vertexCacheOptimizer::calcVertexScore(size_t vertex) {
vertexCacheData *v = &m_vertices[vertex];
if (v->remainingValence == (size_t)-1 || v->remainingValence == 0)
return -1.0f; // No triangle needs this vertex
float value = 0.0f;
if (v->cachePosition == (size_t)-1) {
// Vertex is not in FIFO cache.
} else {
if (v->cachePosition < 3) {
// This vertex was used in the last triangle. It has fixed score
// in whichever of the tree it's in.
value = kLastTriScore;
} else {
// Points for being heigh in the cache
const float scale = 1.0f / (32 - 3);
value = 1.0f - (v->cachePosition - 3) * scale;
value = powf(value, kCacheDecayPower);
}
}
// Bonus points for having a low number of triangles.
float valenceBoost = powf(float(v->remainingValence), -kValenceBoostPower);
value += kValenceBoostScale * valenceBoost;
return value;
}
size_t vertexCacheOptimizer::fullScoreRecalculation() {
// Calculate score for all vertices
for (size_t i = 0; i < m_vertices.size(); i++)
m_vertices[i].currentScore = calcVertexScore(i);
// Calculate scores for all active triangles
float maxScore = 0.0f;
size_t maxScoreTriangle = (size_t)-1;
bool firstTime = true;
for (size_t i = 0; i < m_triangles.size(); i++) {
auto &it = m_triangles[i];
if (it.rendered)
continue;
// Sum the score of all the triangle's vertices
float sum = m_vertices[it.vertices[0]].currentScore +
m_vertices[it.vertices[1]].currentScore +
m_vertices[it.vertices[2]].currentScore;
it.currentScore = sum;
if (firstTime || sum > maxScore) {
firstTime = false;
maxScore = sum;
maxScoreTriangle = i;
}
}
return maxScoreTriangle;
}
vertexCacheOptimizer::result vertexCacheOptimizer::initialPass() {
for (size_t i = 0; i < m_indices.size(); i++) {
size_t index = m_indices[i];
if (index == (size_t)-1 || index >= m_vertices.size())
return kErrorInvalidIndex;
m_vertices[index].totalValence++;
m_vertices[index].remainingValence++;
m_vertices[index].indices.push_back(i / 3);
}
m_bestTriangle = fullScoreRecalculation();
return kSuccess;
}
vertexCacheOptimizer::result vertexCacheOptimizer::init(u::vector<size_t> &indices, size_t maxVertex) {
const size_t triangleCount = indices.size() / 3;
// Reset draw list
m_drawList.clear();
m_drawList.reserve(maxVertex);
// Reset and initialize vertices and triangles
m_vertices.clear();
m_vertices.reserve(maxVertex);
for (size_t i = 0; i < maxVertex; i++)
m_vertices.push_back(vertexCacheData());
m_triangles.clear();
m_triangles.reserve(triangleCount);
for (size_t i = 0; i < triangleCount; i++) {
triangleCacheData data;
for (size_t j = 0; j < 3; j++)
data.vertices[j] = indices[i * 3 + j];
m_triangles.push_back(data);
}
// Copy the indices
m_indices.clear();
m_indices.reserve(indices.size());
for (auto &it : indices)
m_indices.push_back(it);
// Run the initial pass
m_vertexCache.clear();
m_bestTriangle = (size_t)-1;
return initialPass();
}
void vertexCacheOptimizer::addTriangle(size_t triangle) {
// reset all cache positions
for (size_t i = 0; i < 32; i++) {
size_t find = m_vertexCache.getCachedVertex(i);
if (find == (size_t)-1)
continue;
m_vertices[find].cachePosition = (size_t)-1;
}
triangleCacheData *t = &m_triangles[triangle];
if (t->rendered)
return;
for (size_t i = 0; i < 3; i++) {
// Add all the triangle's vertices to the cache
m_vertexCache.addVertex(t->vertices[i]);
vertexCacheData *v = &m_vertices[t->vertices[i]];
// Decrease the remaining valence.
v->remainingValence--;
// Move the added triangle to the end of the vertex's triangle index
// list such that the first `remainingValence' triangles in the index
// list are only the active ones.
v->moveTriangle(triangle);
}
// It's been rendered, mark it
m_drawList.push_back(triangle);
t->rendered = true;
// Update all the vertex cache positions
for (size_t i = 0; i < 32; i++) {
size_t index = m_vertexCache.getCachedVertex(i);
if (index == (size_t)-1)
continue;
m_vertices[index].cachePosition = i;
}
}
// Avoid duplicate calculations during processing. Triangles and vertices have
// a `calculated' flag which must be reset at the beginning of the process for
// all active triangles that have one or more of their vertices currently in
// cache as well all their other vertices.
//
// If there aren't any active triangles in the cache this function returns
// false and a full recalculation of the tree is performed.
bool vertexCacheOptimizer::cleanFlags() {
bool found = false;
for (size_t i = 0; i < 32; i++) {
size_t find = m_vertexCache.getCachedVertex(i);
if (find == (size_t)-1)
continue;
vertexCacheData *v = &m_vertices[find];
for (size_t j = 0; j < v->remainingValence; j++) {
triangleCacheData *t = &m_triangles[v->indices[j]];
found = true;
// Clear flags
t->calculated = false;
for (size_t k = 0; k < 3; k++)
m_vertices[t->vertices[k]].calculated = false;
}
}
return found;
}
void vertexCacheOptimizer::triangleScoreRecalculation(size_t triangle) {
triangleCacheData *t = &m_triangles[triangle];
// Calculate vertex scores
float sum = 0.0f;
for (size_t i = 0; i < 3; i++) {
vertexCacheData *v = &m_vertices[t->vertices[i]];
float score = v->calculated ? v->currentScore : calcVertexScore(t->vertices[i]);
v->currentScore = score;
v->calculated = true;
sum += score;
}
t->currentScore = sum;
t->calculated = true;
}
size_t vertexCacheOptimizer::partialScoreRecalculation() {
// Iterate through all the vertices of the cache
bool firstTime = true;
float maxScore = 0.0f;
size_t maxScoreTriangle = (size_t)-1;
for (size_t i = 0; i < 32; i++) {
size_t find = m_vertexCache.getCachedVertex(i);
if (find == (size_t)-1)
continue;
vertexCacheData *v = &m_vertices[find];
// Iterate through all the active triangles of this vertex
for (size_t j = 0; j < v->remainingValence; j++) {
size_t triangle = v->indices[j];
triangleCacheData *t = &m_triangles[triangle];
// Calculate triangle score if it isn't already calculated
if (!t->calculated)
triangleScoreRecalculation(triangle);
float score = t->currentScore;
// Found a triangle to process
if (firstTime || score > maxScore) {
firstTime = false;
maxScore = score;
maxScoreTriangle = triangle;
}
}
}
return maxScoreTriangle;
}
inline bool vertexCacheOptimizer::process() {
if (m_drawList.size() == m_triangles.size())
return false;
// Add the selected triangle to the draw list
addTriangle(m_bestTriangle);
// Recalculate the vertex and triangle scores and select the best triangle
// for the next iteration.
m_bestTriangle = cleanFlags() ? partialScoreRecalculation() : fullScoreRecalculation();
return true;
}
size_t vertexCacheOptimizer::getCacheMissCount() const {
return m_vertexCache.getCacheMissCount();
}