forked from themrdemonized/xray-monolith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNvTriStrip.cpp
More file actions
278 lines (241 loc) · 8.05 KB
/
NvTriStrip.cpp
File metadata and controls
278 lines (241 loc) · 8.05 KB
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
#include "stdafx.h"
#include "NvTriStripObjects.h"
#include "NvTriStrip.h"
#pragma warning(disable:4018)
////////////////////////////////////////////////////////////////////////////////////////
//private data
static unsigned int cacheSize = CACHESIZE_GEFORCE1_2;
static bool bStitchStrips = true;
static unsigned int minStripSize = 0;
static bool bListsOnly = false;
////////////////////////////////////////////////////////////////////////////////////////
// SetListsOnly()
//
// If set to true, will return an optimized list, with no strips at all.
//
// Default value: false
//
void SetListsOnly(const bool _bListsOnly)
{
bListsOnly = _bListsOnly;
}
////////////////////////////////////////////////////////////////////////////////////////
// SetCacheSize()
//
// Sets the cache size which the stripfier uses to optimize the data.
// Controls the length of the generated individual strips.
// This is the "actual" cache size, so 24 for GeForce3 and 16 for GeForce1/2
// You may want to play around with this number to tweak performance.
//
// Default value: 16
//
void SetCacheSize(const unsigned int _cacheSize)
{
cacheSize = _cacheSize;
}
////////////////////////////////////////////////////////////////////////////////////////
// SetStitchStrips()
//
// bool to indicate whether to stitch together strips into one huge strip or not.
// If set to true, you'll get back one huge strip stitched together using degenerate
// triangles.
// If set to false, you'll get back a large number of separate strips.
//
// Default value: true
//
void SetStitchStrips(const bool _bStitchStrips)
{
bStitchStrips = _bStitchStrips;
}
////////////////////////////////////////////////////////////////////////////////////////
// SetMinStripSize()
//
// Sets the minimum acceptable size for a strip, in triangles.
// All strips generated which are shorter than this will be thrown into one big, separate list.
//
// Default value: 0
//
void SetMinStripSize(const unsigned int _minStripSize)
{
minStripSize = _minStripSize;
}
////////////////////////////////////////////////////////////////////////////////////////
// GenerateStrips()
//
// in_indices: input index list, the indices you would use to render
// in_numIndices: number of entries in in_indices
// primGroups: array of optimized/stripified PrimitiveGroups
// numGroups: number of groups returned
//
// Be sure to call xr_free on the returned primGroups to avoid leaking mem
//
void GenerateStrips(const u16* in_indices, const s32 in_numIndices, xr_vector<PrimitiveGroup>& primGroups)
{
//put data in format that the stripifier likes
WordVec tempIndices;
tempIndices.resize(in_numIndices);
int i;
for (i = 0; i < in_numIndices; i++)
tempIndices[i] = in_indices[i];
NvStripInfoVec tempStrips;
NvFaceInfoVec tempFaces;
NvStripifier stripifier;
//do actual stripification
stripifier.Stripify(tempIndices, cacheSize, minStripSize, tempStrips, tempFaces);
//stitch strips together
IntVec stripIndices;
unsigned int numSeparateStrips = 0;
if (bListsOnly)
{
//if we're outputting only lists, we're done
primGroups.resize(1);
//count the total number of indices
unsigned int numIndices = 0;
for (int i = 0; i < tempStrips.size(); i++)
{
numIndices += tempStrips[i]->m_faces.size() * 3;
}
//add in the list
numIndices += tempFaces.size() * 3;
primGroups[0].type = PT_LIST;
primGroups[0].numIndices = numIndices;
primGroups[0].indices = xr_alloc<u16>(numIndices);
//do strips
unsigned int indexCtr = 0;
for (u32 i = 0; i < tempStrips.size(); i++)
{
for (int j = 0; j < tempStrips[i]->m_faces.size(); j++)
{
primGroups[0].indices[indexCtr++] = u16(tempStrips[i]->m_faces[j]->m_v0);
primGroups[0].indices[indexCtr++] = u16(tempStrips[i]->m_faces[j]->m_v1);
primGroups[0].indices[indexCtr++] = u16(tempStrips[i]->m_faces[j]->m_v2);
}
}
//do lists
for (u32 i = 0; i < tempFaces.size(); i++)
{
primGroups[0].indices[indexCtr++] = u16(tempFaces[i]->m_v0);
primGroups[0].indices[indexCtr++] = u16(tempFaces[i]->m_v1);
primGroups[0].indices[indexCtr++] = u16(tempFaces[i]->m_v2);
}
}
else
{
stripifier.CreateStrips(tempStrips, stripIndices, bStitchStrips, numSeparateStrips);
//if we're stitching strips together, we better get back only one strip from CreateStrips()
assert((bStitchStrips && (numSeparateStrips == 1)) || !bStitchStrips);
//convert to output format
int numGroups = u16(numSeparateStrips); //for the strips
if (tempFaces.size() != 0)
numGroups++; //we've got a list as well, increment
primGroups.resize(numGroups);
//first, the strips
int startingLoc = 0;
int stripCtr;
for (stripCtr = 0; stripCtr < numSeparateStrips; stripCtr++)
{
int stripLength = 0;
if (numSeparateStrips != 1)
{
//if we've got multiple strips, we need to figure out the correct length
int i;
for (i = startingLoc; i < stripIndices.size(); i++)
{
if (stripIndices[i] == -1)
break;
}
stripLength = i - startingLoc;
}
else
stripLength = stripIndices.size();
primGroups[stripCtr].type = PT_STRIP;
primGroups[stripCtr].indices = xr_alloc<u16>(stripLength);
primGroups[stripCtr].numIndices = stripLength;
int indexCtr = 0;
for (int i = startingLoc; i < stripLength + startingLoc; i++)
primGroups[stripCtr].indices[indexCtr++] = u16(stripIndices[i]);
startingLoc += stripLength + 1; //we add 1 to account for the -1 separating strips
}
//next, the list
if (tempFaces.size() != 0)
{
int faceGroupLoc = numGroups - 1; //the face group is the last one
primGroups[faceGroupLoc].type = PT_LIST;
primGroups[faceGroupLoc].indices = xr_alloc<u16>(tempFaces.size() * 3);
primGroups[faceGroupLoc].numIndices = tempFaces.size() * 3;
int indexCtr = 0;
for (int i = 0; i < tempFaces.size(); i++)
{
primGroups[faceGroupLoc].indices[indexCtr++] = u16(tempFaces[i]->m_v0);
primGroups[faceGroupLoc].indices[indexCtr++] = u16(tempFaces[i]->m_v1);
primGroups[faceGroupLoc].indices[indexCtr++] = u16(tempFaces[i]->m_v2);
}
}
}
//clean up everything
//_delete strips
for (u32 i = 0; i < tempStrips.size(); i++)
{
for (int j = 0; j < tempStrips[i]->m_faces.size(); j++)
{
xr_delete(tempStrips[i]->m_faces[j]);
}
xr_delete(tempStrips[i]);
}
//_delete faces
for (u32 i = 0; i < tempFaces.size(); i++)
{
xr_delete(tempFaces[i]);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// RemapIndices()
//
// Function to remap your indices to improve spatial locality in your vertex buffer.
//
// in_primGroups: array of PrimitiveGroups you want remapped
// numGroups: number of entries in in_primGroups
// numVerts: number of vertices in your vertex buffer, also can be thought of as the range
// of acceptable values for indices in your primitive groups.
// remappedGroups: array of remapped PrimitiveGroups
//
// Note that, according to the remapping handed back to you, you must reorder your
// vertex buffer.
//
void RemapIndices(const xr_vector<PrimitiveGroup>& in_primGroups, const u16 numVerts,
xr_vector<PrimitiveGroup>& remappedGroups)
{
int numGroups = in_primGroups.size();
remappedGroups.resize(numGroups);
//caches oldIndex --> newIndex conversion
int* indexCache;
indexCache = xr_alloc<int>(numVerts);
FillMemory(indexCache, sizeof(int)*numVerts, -1);
//loop over primitive groups
unsigned int indexCtr = 0;
for (int i = 0; i < numGroups; i++)
{
unsigned int numIndices = in_primGroups[i].numIndices;
//init remapped group
remappedGroups[i].type = in_primGroups[i].type;
remappedGroups[i].numIndices = numIndices;
remappedGroups[i].indices = xr_alloc<u16>(numIndices);
for (int j = 0; j < numIndices; j++)
{
int cachedIndex = indexCache[in_primGroups[i].indices[j]];
if (cachedIndex == -1) //we haven't seen this index before
{
//point to "last" vertex in VB
remappedGroups[i].indices[j] = u16(indexCtr);
//add to index cache, increment
indexCache[in_primGroups[i].indices[j]] = indexCtr++;
}
else
{
//we've seen this index before
remappedGroups[i].indices[j] = u16(cachedIndex);
}
}
}
xr_free(indexCache);
}