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
|
/*
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include <bx/debug.h>
#include <bx/allocator.h>
#include <bx/radixsort.h>
#include "config.h"
#include "topology.h"
namespace bgfx
{
template<typename IndexT>
static uint32_t topologyConvertTriListFlipWinding(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
{
if (NULL == _dst)
{
return _numIndices;
}
IndexT* dst = (IndexT*)_dst;
IndexT* end = &dst[_dstSize/sizeof(IndexT)];
for (uint32_t ii = 0; ii < _numIndices && dst < end; ii += 3, dst += 3)
{
const IndexT* tri = &_indices[ii];
IndexT i0 = tri[0], i1 = tri[1], i2 = tri[2];
dst[0] = i0;
dst[1] = i2;
dst[2] = i1;
}
return _numIndices;
}
template<typename IndexT, typename SortT>
static uint32_t topologyConvertTriListToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices, IndexT* _temp, SortT* _tempSort)
{
// Create all line pairs and sort indices.
IndexT* dst = _temp;
for (uint32_t ii = 0; ii < _numIndices; ii += 3)
{
const IndexT* tri = &_indices[ii];
IndexT i0 = tri[0], i1 = tri[1], i2 = tri[2];
if (i0 > i1) { bx::xchg(i0, i1); }
if (i1 > i2) { bx::xchg(i1, i2); }
if (i0 > i1) { bx::xchg(i0, i1); }
BX_CHECK(i0 < i1 && i1 < i2, "");
dst[0] = i0; dst[1] = i1;
dst[2] = i1; dst[3] = i2;
dst[4] = i0; dst[5] = i2;
dst += 6;
}
// Sort all line pairs.
SortT* sorted = (SortT*)_temp;
bx::radixSort(sorted, _tempSort, _numIndices);
uint32_t num = 0;
// Remove all line pair duplicates.
if (NULL == _dst)
{
SortT last = sorted[0];
for (uint32_t ii = 1; ii < _numIndices; ++ii)
{
if (last != sorted[ii])
{
num += 2;
last = sorted[ii];
}
}
num += 2;
}
else
{
dst = (IndexT*)_dst;
IndexT* end = &dst[_dstSize/sizeof(IndexT)];
SortT last = sorted[0];
for (uint32_t ii = 1; ii < _numIndices && dst < end; ++ii)
{
if (last != sorted[ii])
{
union Un { SortT key; struct { IndexT i0; IndexT i1; } u16; } un = { last };
dst[0] = un.u16.i0;
dst[1] = un.u16.i1;
dst += 2;
last = sorted[ii];
}
}
if (dst < end)
{
union Un { SortT key; struct { IndexT i0; IndexT i1; } u16; } un = { last };
dst[0] = un.u16.i0;
dst[1] = un.u16.i1;
dst += 2;
}
num = uint32_t(dst - (IndexT*)_dst);
}
return num;
}
template<typename IndexT, typename SortT>
static uint32_t topologyConvertTriListToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices, bx::AllocatorI* _allocator)
{
IndexT* temp = (IndexT*)BX_ALLOC(_allocator, _numIndices*2*sizeof(IndexT)*2);
SortT* tempSort = (SortT*)&temp[_numIndices*2];
uint32_t num = topologyConvertTriListToLineList(_dst, _dstSize, _indices, _numIndices, temp, tempSort);
BX_FREE(_allocator, temp);
return num;
}
template<typename IndexT>
static uint32_t topologyConvertTriStripToTriList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
{
IndexT* dst = (IndexT*)_dst;
IndexT* end = &dst[_dstSize/sizeof(IndexT)];
for (uint32_t ii = 0, num = _numIndices-2; ii < num && dst < end; ++ii)
{
IndexT i0 = _indices[ii+0];
IndexT i1 = _indices[ii+1];
IndexT i2 = _indices[ii+2];
if (i0 != i1
&& i1 != i2)
{
dst[0] = i0;
dst[1] = i1;
dst[2] = i2;
dst += 3;
}
}
return uint32_t(dst - (IndexT*)_dst);
}
template<typename IndexT>
static uint32_t topologyConvertLineStripToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
{
IndexT* dst = (IndexT*)_dst;
IndexT* end = &dst[_dstSize/sizeof(IndexT)];
IndexT i0 = _indices[0];
for (uint32_t ii = 1; ii < _numIndices && dst < end; ++ii)
{
IndexT i1 = _indices[ii];
if (i0 != i1)
{
dst[0] = i0;
dst[1] = i1;
dst += 2;
i0 = i1;
}
}
return uint32_t(dst - (IndexT*)_dst);
}
uint32_t topologyConvert(TopologyConvert::Enum _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32, bx::AllocatorI* _allocator)
{
switch (_conversion)
{
case TopologyConvert::TriStripToTriList:
if (_index32)
{
return topologyConvertTriStripToTriList(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
}
return topologyConvertTriStripToTriList(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
case TopologyConvert::TriListFlipWinding:
if (_index32)
{
return topologyConvertTriListFlipWinding(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
}
return topologyConvertTriListFlipWinding(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
case TopologyConvert::TriListToLineList:
if (NULL == _allocator)
{
return 0;
}
if (_index32)
{
return topologyConvertTriListToLineList<uint32_t, uint64_t>(_dst, _dstSize, (const uint32_t*)_indices, _numIndices, _allocator);
}
return topologyConvertTriListToLineList<uint16_t, uint32_t>(_dst, _dstSize, (const uint16_t*)_indices, _numIndices, _allocator);
case TopologyConvert::LineStripToLineList:
if (_index32)
{
return topologyConvertLineStripToLineList(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
}
return topologyConvertLineStripToLineList(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
default:
break;
}
return 0;
}
} //namespace bgfx
|