-
Notifications
You must be signed in to change notification settings - Fork 0
/
jrc_forsyth.h
420 lines (357 loc) · 12.3 KB
/
jrc_forsyth.h
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//-----------------------------------------------------------------------------
// This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache
// Optimization" algorithm as described here:
// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
//
// This code was authored and released into the public domain by
// Adrian Stone ([email protected]).
//
// Backported to C, OptimizeVertexes() added, and made into a single header by
// James Canete.
//
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER
// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef JRC_FORSYTH_H
#define JRC_FORSYTH_H
typedef unsigned int fIndex_t;
enum { MAX_INDEX = 4294967295u};
//-----------------------------------------------------------------------------
// OptimizeFaces
//-----------------------------------------------------------------------------
// Parameters:
// indexList
// input index list
// indexCount
// the number of indices in the list
// vertexCount
// the largest index value in indexList
// newIndexList
// a pointer to a preallocated buffer the same size as indexList to
// hold the optimized index list
// lruCacheSize
// the size of the simulated post-transform cache (max:64)
//-----------------------------------------------------------------------------
void OptimizeFaces(const fIndex_t* indexList, unsigned int indexCount, unsigned int vertexCount, fIndex_t* newIndexList, fIndex_t lruCacheSize);
//-----------------------------------------------------------------------------
// OptimizeVertexes
//-----------------------------------------------------------------------------
// Parameters:
// vertexList
// input vertex list
// vertexCount
// the largest index value in indexList
// vertexSize
// the size, in bytes, of each vertex
// indexList
// input index list
// indexCount
// the number of indices in the list
// newVertexList
// a pointer to a preallocated buffer the same size as vertexList to
// hold the optimized vertex list
// newIndexList
// a pointer to a preallocated buffer the same size as indexList to
// hold the modified index list
//-----------------------------------------------------------------------------
void OptimizeVertexes(const void *vertexList, unsigned int vertexCount, unsigned int vertexSize, fIndex_t *indexList, unsigned int indexCount, void *newVertexList, fIndex_t *newIndexList);
#endif
#ifdef JRC_FORSYTH_IMPLEMENTATION
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int uint;
// code for computing vertex score was taken, as much as possible
// directly from the original publication.
static float ComputeVertexCacheScore(int cachePosition, int vertexCacheSize)
{
const float FindVertexScore_CacheDecayPower = 1.5f;
const float FindVertexScore_LastTriScore = 0.75f;
float score = 0.0f;
if ( cachePosition < 0 )
{
// Vertex is not in FIFO cache - no score.
}
else
{
if ( cachePosition < 3 )
{
// This vertex was used in the last triangle,
// so it has a fixed score, whichever of the three
// it's in. Otherwise, you can get very different
// answers depending on whether you add
// the triangle 1,2,3 or 3,1,2 - which is silly.
score = FindVertexScore_LastTriScore;
}
else
{
assert ( cachePosition < vertexCacheSize );
// Points for being high in the cache.
const float scaler = 1.0f / ( vertexCacheSize - 3 );
score = 1.0f - ( cachePosition - 3 ) * scaler;
score = powf ( score, FindVertexScore_CacheDecayPower );
}
}
return score;
}
static float ComputeVertexValenceScore(uint numActiveFaces)
{
const float FindVertexScore_ValenceBoostScale = 2.0f;
const float FindVertexScore_ValenceBoostPower = 0.5f;
float score = 0.f;
// Bonus points for having a low number of tris still to
// use the vert, so we get rid of lone verts quickly.
float valenceBoost = powf ( (float)(numActiveFaces),
-FindVertexScore_ValenceBoostPower );
score += FindVertexScore_ValenceBoostScale * valenceBoost;
return score;
}
enum {kMaxVertexCacheSize = 64};
enum {kMaxPrecomputedVertexValenceScores = 64};
static float s_vertexCacheScores[kMaxVertexCacheSize+1][kMaxVertexCacheSize];
static float s_vertexValenceScores[kMaxPrecomputedVertexValenceScores];
static char ComputeVertexScores()
{
int cacheSize, cachePos;
uint valence;
for (cacheSize=0; cacheSize<=kMaxVertexCacheSize; ++cacheSize)
{
for (cachePos=0; cachePos<cacheSize; ++cachePos)
{
s_vertexCacheScores[cacheSize][cachePos] = ComputeVertexCacheScore(cachePos, cacheSize);
}
}
for (valence=0; valence<kMaxPrecomputedVertexValenceScores; ++valence)
{
s_vertexValenceScores[valence] = ComputeVertexValenceScore(valence);
}
return 1;
}
static char s_vertexScoresComputed = 0;
// inline float FindVertexCacheScore(uint cachePosition, uint maxSizeVertexCache)
// {
// return s_vertexCacheScores[maxSizeVertexCache][cachePosition];
// }
// inline float FindVertexValenceScore(uint numActiveTris)
// {
// return s_vertexValenceScores[numActiveTris];
// }
float FindVertexScore(uint numActiveFaces, uint cachePosition, uint vertexCacheSize)
{
if (!s_vertexScoresComputed)
s_vertexScoresComputed = ComputeVertexScores();
if ( numActiveFaces == 0 )
{
// No tri needs this vertex!
return -1.0f;
}
float score = 0.f;
if (cachePosition < vertexCacheSize)
{
score += s_vertexCacheScores[vertexCacheSize][cachePosition];
}
if (numActiveFaces < kMaxPrecomputedVertexValenceScores)
{
score += s_vertexValenceScores[numActiveFaces];
}
else
{
score += ComputeVertexValenceScore(numActiveFaces);
}
return score;
}
typedef struct
{
float score;
uint activeFaceListStart;
uint activeFaceListSize;
fIndex_t cachePos0;
fIndex_t cachePos1;
}
OptimizeVertexData;
void OptimizeFaces(const fIndex_t* indexList, unsigned int indexCount, unsigned int vertexCount, fIndex_t* newIndexList, fIndex_t lruCacheSize)
{
OptimizeVertexData *vertexDataList;
uint i, j, k, v, c0, c1;
vertexDataList = calloc(vertexCount, sizeof(*vertexDataList));
// compute face count per vertex
for (i=0; i<indexCount; ++i)
{
fIndex_t index = indexList[i];
assert(index < vertexCount);
vertexDataList[index].activeFaceListSize++;
}
uint *activeFaceList;
const fIndex_t kEvictedCacheIndex = MAX_INDEX;
{
// allocate face list per vertex
uint curActiveFaceListPos = 0;
for (i=0; i<vertexCount; ++i)
{
OptimizeVertexData* vertexData = &vertexDataList[i];
vertexData->cachePos0 = kEvictedCacheIndex;
vertexData->cachePos1 = kEvictedCacheIndex;
vertexData->activeFaceListStart = curActiveFaceListPos;
curActiveFaceListPos += vertexData->activeFaceListSize;
vertexData->score = FindVertexScore(vertexData->activeFaceListSize, vertexData->cachePos0, lruCacheSize);
vertexData->activeFaceListSize = 0;
}
activeFaceList = malloc(curActiveFaceListPos * sizeof(*activeFaceList));
}
// fill out face list per vertex
for (i=0; i<indexCount; i+=3)
{
for (j=0; j<3; ++j)
{
fIndex_t index = indexList[i+j];
OptimizeVertexData* vertexData = &vertexDataList[index];
activeFaceList[vertexData->activeFaceListStart + vertexData->activeFaceListSize] = i;
vertexData->activeFaceListSize++;
}
}
char *processedFaceList;
processedFaceList = calloc(indexCount, sizeof(*processedFaceList));
fIndex_t vertexCacheBuffer[(kMaxVertexCacheSize+3)*2];
fIndex_t* cache0 = vertexCacheBuffer;
fIndex_t* cache1 = vertexCacheBuffer+(kMaxVertexCacheSize+3);
fIndex_t entriesInCache0 = 0;
uint bestFace = 0;
float bestScore = -1.f;
const float maxValenceScore = FindVertexScore(1, kEvictedCacheIndex, lruCacheSize) * 3.f;
for (i = 0; i < indexCount; i += 3)
{
if (bestScore < 0.f)
{
// no verts in the cache are used by any unprocessed faces so
// search all unprocessed faces for a new starting point
for (j = 0; j < indexCount; j += 3)
{
if (processedFaceList[j] == 0)
{
uint face = j;
float faceScore = 0.f;
for (k=0; k<3; ++k)
{
fIndex_t index = indexList[face+k];
OptimizeVertexData* vertexData = &vertexDataList[index];
assert(vertexData->activeFaceListSize > 0);
assert(vertexData->cachePos0 >= lruCacheSize);
faceScore += vertexData->score;
}
if (faceScore > bestScore)
{
bestScore = faceScore;
bestFace = face;
assert(bestScore <= maxValenceScore);
if (bestScore >= maxValenceScore)
{
break;
}
}
}
}
assert(bestScore >= 0.f);
}
processedFaceList[bestFace] = 1;
fIndex_t entriesInCache1 = 0;
// add bestFace to LRU cache and to newIndexList
for (v = 0; v < 3; ++v)
{
fIndex_t index = indexList[bestFace+v];
newIndexList[i+v] = index;
OptimizeVertexData* vertexData = &vertexDataList[index];
if (vertexData->cachePos1 >= entriesInCache1)
{
vertexData->cachePos1 = entriesInCache1;
cache1[entriesInCache1++] = index;
if (vertexData->activeFaceListSize == 1)
{
--vertexData->activeFaceListSize;
continue;
}
}
assert(vertexData->activeFaceListSize > 0);
uint* begin = &activeFaceList[vertexData->activeFaceListStart];
uint* end = &activeFaceList[vertexData->activeFaceListStart + vertexData->activeFaceListSize];
//uint* it = std::find(begin, end, bestFace);
uint* it;
for (it = begin; *it != bestFace && it != end; it++);
assert(it != end);
//std::swap(*it, *(end-1));
{
uint tmp = *it;
*it = *(end-1);
*(end-1)=tmp;
}
--vertexData->activeFaceListSize;
vertexData->score = FindVertexScore(vertexData->activeFaceListSize, vertexData->cachePos1, lruCacheSize);
}
// move the rest of the old verts in the cache down and compute their new scores
for (c0 = 0; c0 < entriesInCache0; ++c0)
{
fIndex_t index = cache0[c0];
OptimizeVertexData* vertexData = &vertexDataList[index];
if (vertexData->cachePos1 >= entriesInCache1)
{
vertexData->cachePos1 = entriesInCache1;
cache1[entriesInCache1++] = index;
vertexData->score = FindVertexScore(vertexData->activeFaceListSize, vertexData->cachePos1, lruCacheSize);
}
}
// find the best scoring triangle in the current cache (including up to 3 that were just evicted)
bestScore = -1.f;
for (c1 = 0; c1 < entriesInCache1; ++c1)
{
fIndex_t index = cache1[c1];
OptimizeVertexData* vertexData = &vertexDataList[index];
vertexData->cachePos0 = vertexData->cachePos1;
vertexData->cachePos1 = kEvictedCacheIndex;
for (j=0; j<vertexData->activeFaceListSize; ++j)
{
uint face = activeFaceList[vertexData->activeFaceListStart+j];
float faceScore = 0.f;
for (v=0; v<3; v++)
{
fIndex_t faceIndex = indexList[face+v];
faceScore += vertexDataList[faceIndex].score;
}
if (faceScore > bestScore)
{
bestScore = faceScore;
bestFace = face;
}
}
}
//std::swap(cache0, cache1);
{
fIndex_t* tmp = cache0;
cache0 = cache1;
cache1 = tmp;
}
//entriesInCache0 = std::min(entriesInCache1, lruCacheSize);
entriesInCache0 = entriesInCache1 < lruCacheSize ? entriesInCache1 : lruCacheSize;
}
free(vertexDataList);
free(processedFaceList);
free(activeFaceList);
}
void OptimizeVertexes(const void *vertexList, unsigned int vertexCount, unsigned int vertexSize, fIndex_t *indexList, unsigned int indexCount, void *newVertexList, fIndex_t *newIndexList)
{
int i;
fIndex_t lastVertex = 0;
fIndex_t *vertexRemap = calloc(vertexCount, sizeof(fIndex_t));
for (i = 0; i < indexCount; i++)
if (vertexRemap[indexList[i]] == 0)
vertexRemap[indexList[i]] = ++lastVertex;
for (i = 0; i < indexCount; i++)
newIndexList[i] = vertexRemap[indexList[i]] - 1;
for (i = 0; i < vertexCount; i++)
memcpy((char *)(newVertexList) + vertexSize * (vertexRemap[i] - 1), (char *)(vertexList) + vertexSize * i, vertexSize);
free(vertexRemap);
}
#endif