topology.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bx/allocator.h>
  6. #include <bx/debug.h>
  7. #include <bx/math.h>
  8. #include <bx/sort.h>
  9. #include <bx/uint32_t.h>
  10. #include "config.h"
  11. #include "topology.h"
  12. namespace bgfx
  13. {
  14. template<typename IndexT>
  15. static uint32_t topologyConvertTriListFlipWinding(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
  16. {
  17. if (NULL == _dst)
  18. {
  19. return _numIndices;
  20. }
  21. IndexT* dst = (IndexT*)_dst;
  22. IndexT* end = &dst[_dstSize/sizeof(IndexT)];
  23. for (uint32_t ii = 0; ii < _numIndices && dst < end; ii += 3, dst += 3)
  24. {
  25. const IndexT* tri = &_indices[ii];
  26. IndexT i0 = tri[0], i1 = tri[1], i2 = tri[2];
  27. dst[0] = i0;
  28. dst[1] = i2;
  29. dst[2] = i1;
  30. }
  31. return _numIndices;
  32. }
  33. inline bool isEven(uint32_t _num)
  34. {
  35. return 0 == (_num & 1);
  36. }
  37. template<typename IndexT>
  38. static uint32_t topologyConvertTriStripFlipWinding(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
  39. {
  40. const uint32_t numIndices = isEven(_numIndices) ? _numIndices + 1 : _numIndices;
  41. if (NULL != _dst)
  42. {
  43. return numIndices;
  44. }
  45. IndexT* dst = (IndexT*)_dst;
  46. IndexT* end = &dst[_dstSize/sizeof(IndexT)];
  47. if (isEven(_numIndices) )
  48. {
  49. *dst++ = _indices[_numIndices-1];
  50. }
  51. for (uint32_t ii = 1; ii <= _numIndices && dst < end; ++ii)
  52. {
  53. *dst++ = _indices[_numIndices - ii];
  54. }
  55. return numIndices;
  56. }
  57. template<typename IndexT, typename SortT>
  58. static uint32_t topologyConvertTriListToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices, IndexT* _temp, SortT* _tempSort)
  59. {
  60. // Create all line pairs and sort indices.
  61. IndexT* dst = _temp;
  62. for (uint32_t ii = 0; ii < _numIndices; ii += 3)
  63. {
  64. const IndexT* tri = &_indices[ii];
  65. IndexT i0 = tri[0], i1 = tri[1], i2 = tri[2];
  66. if (i0 > i1) { bx::swap(i0, i1); }
  67. if (i1 > i2) { bx::swap(i1, i2); }
  68. if (i0 > i1) { bx::swap(i0, i1); }
  69. BX_CHECK(i0 < i1 && i1 < i2, "");
  70. dst[1] = i0; dst[0] = i1;
  71. dst[3] = i1; dst[2] = i2;
  72. dst[5] = i0; dst[4] = i2;
  73. dst += 6;
  74. }
  75. // Sort all line pairs.
  76. SortT* sorted = (SortT*)_temp;
  77. bx::radixSort(sorted, _tempSort, _numIndices);
  78. uint32_t num = 0;
  79. // Remove all line pair duplicates.
  80. if (NULL == _dst)
  81. {
  82. SortT last = sorted[0];
  83. for (uint32_t ii = 1; ii < _numIndices; ++ii)
  84. {
  85. if (last != sorted[ii])
  86. {
  87. num += 2;
  88. last = sorted[ii];
  89. }
  90. }
  91. num += 2;
  92. }
  93. else
  94. {
  95. dst = (IndexT*)_dst;
  96. IndexT* end = &dst[_dstSize/sizeof(IndexT)];
  97. SortT last = sorted[0];
  98. {
  99. union Un { SortT key; struct { IndexT i0; IndexT i1; } u16; } un = { sorted[0] };
  100. dst[0] = un.u16.i0;
  101. dst[1] = un.u16.i1;
  102. dst += 2;
  103. }
  104. for (uint32_t ii = 1; ii < _numIndices && dst < end; ++ii)
  105. {
  106. if (last != sorted[ii])
  107. {
  108. union Un { SortT key; struct { IndexT i0; IndexT i1; } u16; } un = { sorted[ii] };
  109. dst[0] = un.u16.i0;
  110. dst[1] = un.u16.i1;
  111. dst += 2;
  112. last = sorted[ii];
  113. }
  114. }
  115. num = uint32_t(dst - (IndexT*)_dst);
  116. }
  117. return num;
  118. }
  119. template<typename IndexT, typename SortT>
  120. static uint32_t topologyConvertTriListToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices, bx::AllocatorI* _allocator)
  121. {
  122. IndexT* temp = (IndexT*)BX_ALLOC(_allocator, _numIndices*2*sizeof(IndexT)*2);
  123. SortT* tempSort = (SortT*)&temp[_numIndices*2];
  124. uint32_t num = topologyConvertTriListToLineList(_dst, _dstSize, _indices, _numIndices, temp, tempSort);
  125. BX_FREE(_allocator, temp);
  126. return num;
  127. }
  128. template<typename IndexT>
  129. static uint32_t topologyConvertTriStripToTriList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
  130. {
  131. IndexT* dst = (IndexT*)_dst;
  132. IndexT* end = &dst[_dstSize/sizeof(IndexT)];
  133. for (uint32_t ii = 0, num = _numIndices-2; ii < num && dst < end; ++ii)
  134. {
  135. IndexT i0 = _indices[ii+0];
  136. IndexT i1 = _indices[ii+1];
  137. IndexT i2 = _indices[ii+2];
  138. if (i0 != i1
  139. && i1 != i2)
  140. {
  141. dst[0] = i0;
  142. dst[1] = i1;
  143. dst[2] = i2;
  144. dst += 3;
  145. }
  146. }
  147. return uint32_t(dst - (IndexT*)_dst);
  148. }
  149. template<typename IndexT>
  150. static uint32_t topologyConvertLineStripToLineList(void* _dst, uint32_t _dstSize, const IndexT* _indices, uint32_t _numIndices)
  151. {
  152. IndexT* dst = (IndexT*)_dst;
  153. IndexT* end = &dst[_dstSize/sizeof(IndexT)];
  154. IndexT i0 = _indices[0];
  155. for (uint32_t ii = 1; ii < _numIndices && dst < end; ++ii)
  156. {
  157. IndexT i1 = _indices[ii];
  158. if (i0 != i1)
  159. {
  160. dst[0] = i0;
  161. dst[1] = i1;
  162. dst += 2;
  163. i0 = i1;
  164. }
  165. }
  166. return uint32_t(dst - (IndexT*)_dst);
  167. }
  168. uint32_t topologyConvert(
  169. TopologyConvert::Enum _conversion
  170. , void* _dst
  171. , uint32_t _dstSize
  172. , const void* _indices
  173. , uint32_t _numIndices
  174. , bool _index32
  175. , bx::AllocatorI* _allocator
  176. )
  177. {
  178. switch (_conversion)
  179. {
  180. case TopologyConvert::TriStripToTriList:
  181. if (_index32)
  182. {
  183. return topologyConvertTriStripToTriList(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
  184. }
  185. return topologyConvertTriStripToTriList(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
  186. case TopologyConvert::TriListFlipWinding:
  187. if (_index32)
  188. {
  189. return topologyConvertTriListFlipWinding(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
  190. }
  191. return topologyConvertTriListFlipWinding(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
  192. case TopologyConvert::TriStripFlipWinding:
  193. if (_index32)
  194. {
  195. return topologyConvertTriStripFlipWinding(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
  196. }
  197. return topologyConvertTriStripFlipWinding(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
  198. case TopologyConvert::TriListToLineList:
  199. if (NULL == _allocator)
  200. {
  201. return 0;
  202. }
  203. if (_index32)
  204. {
  205. return topologyConvertTriListToLineList<uint32_t, uint64_t>(_dst, _dstSize, (const uint32_t*)_indices, _numIndices, _allocator);
  206. }
  207. return topologyConvertTriListToLineList<uint16_t, uint32_t>(_dst, _dstSize, (const uint16_t*)_indices, _numIndices, _allocator);
  208. case TopologyConvert::LineStripToLineList:
  209. if (_index32)
  210. {
  211. return topologyConvertLineStripToLineList(_dst, _dstSize, (const uint32_t*)_indices, _numIndices);
  212. }
  213. return topologyConvertLineStripToLineList(_dst, _dstSize, (const uint16_t*)_indices, _numIndices);
  214. default:
  215. break;
  216. }
  217. return 0;
  218. }
  219. inline float fmin3(float _a, float _b, float _c)
  220. {
  221. return bx::min(_a, _b, _c);
  222. }
  223. inline float fmax3(float _a, float _b, float _c)
  224. {
  225. return bx::max(_a, _b, _c);
  226. }
  227. inline float favg3(float _a, float _b, float _c)
  228. {
  229. return (_a + _b + _c) * 1.0f/3.0f;
  230. }
  231. const bx::Vec3 vertexPos(const void* _vertices, uint32_t _stride, uint32_t _index)
  232. {
  233. const uint8_t* vertices = (const uint8_t*)_vertices;
  234. return bx::load<bx::Vec3>(&vertices[_index*_stride]);
  235. }
  236. inline float distanceDir(const float* __restrict _dir, const void* __restrict _vertices, uint32_t _stride, uint32_t _index)
  237. {
  238. return bx::dot(vertexPos(_vertices, _stride, _index), bx::load<bx::Vec3>(_dir) );
  239. }
  240. inline float distancePos(const float* __restrict _pos, const void* __restrict _vertices, uint32_t _stride, uint32_t _index)
  241. {
  242. const bx::Vec3 tmp = bx::sub(bx::load<bx::Vec3>(_pos), vertexPos(_vertices, _stride, _index) );
  243. return bx::sqrt(bx::dot(tmp, tmp) );
  244. }
  245. typedef float (*KeyFn)(float, float, float);
  246. typedef float (*DistanceFn)(const float*, const void*, uint32_t, uint32_t);
  247. template<typename IndexT, DistanceFn dfn, KeyFn kfn, uint32_t xorBits>
  248. inline void calcSortKeys(
  249. uint32_t* __restrict _keys
  250. , uint32_t* __restrict _values
  251. , const float _dirOrPos[3]
  252. , const void* __restrict _vertices
  253. , uint32_t _stride
  254. , const IndexT* _indices
  255. , uint32_t _num
  256. )
  257. {
  258. for (uint32_t ii = 0; ii < _num; ++ii)
  259. {
  260. const uint32_t idx0 = _indices[0];
  261. const uint32_t idx1 = _indices[1];
  262. const uint32_t idx2 = _indices[2];
  263. _indices += 3;
  264. float distance0 = dfn(_dirOrPos, _vertices, _stride, idx0);
  265. float distance1 = dfn(_dirOrPos, _vertices, _stride, idx1);
  266. float distance2 = dfn(_dirOrPos, _vertices, _stride, idx2);
  267. uint32_t ui = bx::floatToBits(kfn(distance0, distance1, distance2) );
  268. _keys[ii] = bx::floatFlip(ui) ^ xorBits;
  269. _values[ii] = ii;
  270. }
  271. }
  272. template<typename IndexT>
  273. void topologySortTriList(
  274. TopologySort::Enum _sort
  275. , IndexT* _dst
  276. , uint32_t* _keys
  277. , uint32_t* _values
  278. , uint32_t* _tempKeys
  279. , uint32_t* _tempValues
  280. , uint32_t _num
  281. , const float _dir[3]
  282. , const float _pos[3]
  283. , const void* _vertices
  284. , uint32_t _stride
  285. , const IndexT* _indices
  286. )
  287. {
  288. using namespace bx;
  289. switch (_sort)
  290. {
  291. default:
  292. case TopologySort::DirectionFrontToBackMin: calcSortKeys<IndexT, distanceDir, fmin3, 0 >(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  293. case TopologySort::DirectionFrontToBackAvg: calcSortKeys<IndexT, distanceDir, favg3, 0 >(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  294. case TopologySort::DirectionFrontToBackMax: calcSortKeys<IndexT, distanceDir, fmax3, 0 >(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  295. case TopologySort::DirectionBackToFrontMin: calcSortKeys<IndexT, distanceDir, fmin3, UINT32_MAX>(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  296. case TopologySort::DirectionBackToFrontAvg: calcSortKeys<IndexT, distanceDir, favg3, UINT32_MAX>(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  297. case TopologySort::DirectionBackToFrontMax: calcSortKeys<IndexT, distanceDir, fmax3, UINT32_MAX>(_keys, _values, _dir, _vertices, _stride, _indices, _num); break;
  298. case TopologySort::DistanceFrontToBackMin: calcSortKeys<IndexT, distancePos, fmin3, 0 >(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  299. case TopologySort::DistanceFrontToBackAvg: calcSortKeys<IndexT, distancePos, favg3, 0 >(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  300. case TopologySort::DistanceFrontToBackMax: calcSortKeys<IndexT, distancePos, fmax3, 0 >(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  301. case TopologySort::DistanceBackToFrontMin: calcSortKeys<IndexT, distancePos, fmin3, UINT32_MAX>(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  302. case TopologySort::DistanceBackToFrontAvg: calcSortKeys<IndexT, distancePos, favg3, UINT32_MAX>(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  303. case TopologySort::DistanceBackToFrontMax: calcSortKeys<IndexT, distancePos, fmax3, UINT32_MAX>(_keys, _values, _pos, _vertices, _stride, _indices, _num); break;
  304. }
  305. radixSort(_keys, _tempKeys, _values, _tempValues, _num);
  306. IndexT* sorted = _dst;
  307. for (uint32_t ii = 0; ii < _num; ++ii)
  308. {
  309. uint32_t face = _values[ii]*3;
  310. const IndexT idx0 = _indices[face+0];
  311. const IndexT idx1 = _indices[face+1];
  312. const IndexT idx2 = _indices[face+2];
  313. sorted[0] = idx0;
  314. sorted[1] = idx1;
  315. sorted[2] = idx2;
  316. sorted += 3;
  317. }
  318. }
  319. void topologySortTriList(
  320. TopologySort::Enum _sort
  321. , void* _dst
  322. , uint32_t _dstSize
  323. , const float _dir[3]
  324. , const float _pos[3]
  325. , const void* _vertices
  326. , uint32_t _stride
  327. , const void* _indices
  328. , uint32_t _numIndices
  329. , bool _index32
  330. , bx::AllocatorI* _allocator
  331. )
  332. {
  333. uint32_t indexSize = _index32
  334. ? sizeof(uint32_t)
  335. : sizeof(uint16_t)
  336. ;
  337. uint32_t num = bx::uint32_min(_numIndices*indexSize, _dstSize)/(indexSize*3);
  338. uint32_t* temp = (uint32_t*)BX_ALLOC(_allocator, sizeof(uint32_t)*num*4);
  339. uint32_t* keys = &temp[num*0];
  340. uint32_t* values = &temp[num*1];
  341. uint32_t* tempKeys = &temp[num*2];
  342. uint32_t* tempValues = &temp[num*3];
  343. if (_index32)
  344. {
  345. topologySortTriList(
  346. _sort
  347. , (uint32_t*)_dst
  348. , keys
  349. , values
  350. , tempKeys
  351. , tempValues
  352. , num
  353. , _dir
  354. , _pos
  355. , _vertices
  356. , _stride
  357. , (const uint32_t*)_indices
  358. );
  359. }
  360. else
  361. {
  362. topologySortTriList(
  363. _sort
  364. , (uint16_t*)_dst
  365. , keys
  366. , values
  367. , tempKeys
  368. , tempValues
  369. , num
  370. , _dir
  371. , _pos
  372. , _vertices
  373. , _stride
  374. , (const uint16_t*)_indices
  375. );
  376. }
  377. BX_FREE(_allocator, temp);
  378. }
  379. } //namespace bgfx