spatialorder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
  2. #include "meshoptimizer.h"
  3. #include <assert.h>
  4. #include <float.h>
  5. #include <string.h>
  6. // This work is based on:
  7. // Fabian Giesen. Decoding Morton codes. 2009
  8. namespace meshopt
  9. {
  10. // "Insert" two 0 bits after each of the 20 low bits of x
  11. inline unsigned long long part1By2(unsigned long long x)
  12. {
  13. x &= 0x000fffffull; // x = ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- jihg fedc ba98 7654 3210
  14. x = (x ^ (x << 32)) & 0x000f00000000ffffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- ---- ---- ---- ---- fedc ba98 7654 3210
  15. x = (x ^ (x << 16)) & 0x000f0000ff0000ffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- fedc ba98 ---- ---- ---- ---- 7654 3210
  16. x = (x ^ (x << 8)) & 0x000f00f00f00f00full; // x = ---- ---- ---- jihg ---- ---- fedc ---- ---- ba98 ---- ---- 7654 ---- ---- 3210
  17. x = (x ^ (x << 4)) & 0x00c30c30c30c30c3ull; // x = ---- ---- ji-- --hg ---- fe-- --dc ---- ba-- --98 ---- 76-- --54 ---- 32-- --10
  18. x = (x ^ (x << 2)) & 0x0249249249249249ull; // x = ---- --j- -i-- h--g --f- -e-- d--c --b- -a-- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
  19. return x;
  20. }
  21. static void computeOrder(unsigned long long* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, bool morton)
  22. {
  23. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  24. float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
  25. float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
  26. for (size_t i = 0; i < vertex_count; ++i)
  27. {
  28. const float* v = vertex_positions_data + i * vertex_stride_float;
  29. for (int j = 0; j < 3; ++j)
  30. {
  31. float vj = v[j];
  32. minv[j] = minv[j] > vj ? vj : minv[j];
  33. maxv[j] = maxv[j] < vj ? vj : maxv[j];
  34. }
  35. }
  36. float extent = 0.f;
  37. extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
  38. extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
  39. extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
  40. // rescale each axis to 16 bits to get 48-bit Morton codes
  41. float scale = extent == 0 ? 0.f : 65535.f / extent;
  42. // generate Morton order based on the position inside a unit cube
  43. for (size_t i = 0; i < vertex_count; ++i)
  44. {
  45. const float* v = vertex_positions_data + i * vertex_stride_float;
  46. int x = int((v[0] - minv[0]) * scale + 0.5f);
  47. int y = int((v[1] - minv[1]) * scale + 0.5f);
  48. int z = int((v[2] - minv[2]) * scale + 0.5f);
  49. if (morton)
  50. result[i] = part1By2(x) | (part1By2(y) << 1) | (part1By2(z) << 2);
  51. else
  52. result[i] = ((unsigned long long)x << 0) | ((unsigned long long)y << 20) | ((unsigned long long)z << 40);
  53. }
  54. }
  55. static void radixSort10(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count)
  56. {
  57. unsigned int hist[1024];
  58. memset(hist, 0, sizeof(hist));
  59. // compute histogram (assume keys are 10-bit)
  60. for (size_t i = 0; i < count; ++i)
  61. hist[keys[i]]++;
  62. unsigned int sum = 0;
  63. // replace histogram data with prefix histogram sums in-place
  64. for (int i = 0; i < 1024; ++i)
  65. {
  66. unsigned int h = hist[i];
  67. hist[i] = sum;
  68. sum += h;
  69. }
  70. assert(sum == count);
  71. // reorder values
  72. for (size_t i = 0; i < count; ++i)
  73. {
  74. unsigned int id = keys[source[i]];
  75. destination[hist[id]++] = source[i];
  76. }
  77. }
  78. static void computeHistogram(unsigned int (&hist)[256][2], const unsigned short* data, size_t count)
  79. {
  80. memset(hist, 0, sizeof(hist));
  81. // compute 2 8-bit histograms in parallel
  82. for (size_t i = 0; i < count; ++i)
  83. {
  84. unsigned long long id = data[i];
  85. hist[(id >> 0) & 255][0]++;
  86. hist[(id >> 8) & 255][1]++;
  87. }
  88. unsigned int sum0 = 0, sum1 = 0;
  89. // replace histogram data with prefix histogram sums in-place
  90. for (int i = 0; i < 256; ++i)
  91. {
  92. unsigned int h0 = hist[i][0], h1 = hist[i][1];
  93. hist[i][0] = sum0;
  94. hist[i][1] = sum1;
  95. sum0 += h0;
  96. sum1 += h1;
  97. }
  98. assert(sum0 == count && sum1 == count);
  99. }
  100. static void radixPass(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count, unsigned int (&hist)[256][2], int pass)
  101. {
  102. int bitoff = pass * 8;
  103. for (size_t i = 0; i < count; ++i)
  104. {
  105. unsigned int id = unsigned(keys[source[i]] >> bitoff) & 255;
  106. destination[hist[id][pass]++] = source[i];
  107. }
  108. }
  109. static void partitionPoints(unsigned int* target, const unsigned int* order, const unsigned char* sides, size_t split, size_t count)
  110. {
  111. size_t l = 0, r = split;
  112. for (size_t i = 0; i < count; ++i)
  113. {
  114. unsigned char side = sides[order[i]];
  115. target[side ? r : l] = order[i];
  116. l += 1;
  117. l -= side;
  118. r += side;
  119. }
  120. assert(l == split && r == count);
  121. }
  122. static void splitPoints(unsigned int* destination, unsigned int* orderx, unsigned int* ordery, unsigned int* orderz, const unsigned long long* keys, size_t count, void* scratch, size_t cluster_size)
  123. {
  124. if (count <= cluster_size)
  125. {
  126. memcpy(destination, orderx, count * sizeof(unsigned int));
  127. return;
  128. }
  129. unsigned int* axes[3] = {orderx, ordery, orderz};
  130. int bestk = -1;
  131. unsigned int bestdim = 0;
  132. for (int k = 0; k < 3; ++k)
  133. {
  134. const unsigned int mask = (1 << 20) - 1;
  135. unsigned int dim = (unsigned(keys[axes[k][count - 1]] >> (k * 20)) & mask) - (unsigned(keys[axes[k][0]] >> (k * 20)) & mask);
  136. if (dim >= bestdim)
  137. {
  138. bestk = k;
  139. bestdim = dim;
  140. }
  141. }
  142. assert(bestk >= 0);
  143. // split roughly in half, with the left split always being aligned to cluster size
  144. size_t split = ((count / 2) + cluster_size - 1) / cluster_size * cluster_size;
  145. assert(split > 0 && split < count);
  146. // mark sides of split for partitioning
  147. unsigned char* sides = static_cast<unsigned char*>(scratch) + count * sizeof(unsigned int);
  148. for (size_t i = 0; i < split; ++i)
  149. sides[axes[bestk][i]] = 0;
  150. for (size_t i = split; i < count; ++i)
  151. sides[axes[bestk][i]] = 1;
  152. // partition all axes into two sides, maintaining order
  153. unsigned int* temp = static_cast<unsigned int*>(scratch);
  154. for (int k = 0; k < 3; ++k)
  155. {
  156. if (k == bestk)
  157. continue;
  158. unsigned int* axis = axes[k];
  159. memcpy(temp, axis, sizeof(unsigned int) * count);
  160. partitionPoints(axis, temp, sides, split, count);
  161. }
  162. // recursion depth is logarithmic and bounded as we always split in approximately half
  163. splitPoints(destination, orderx, ordery, orderz, keys, split, scratch, cluster_size);
  164. splitPoints(destination + split, orderx + split, ordery + split, orderz + split, keys, count - split, scratch, cluster_size);
  165. }
  166. } // namespace meshopt
  167. void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  168. {
  169. using namespace meshopt;
  170. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  171. assert(vertex_positions_stride % sizeof(float) == 0);
  172. meshopt_Allocator allocator;
  173. unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);
  174. computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ true);
  175. unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 2b for keys
  176. unsigned short* keyk = (unsigned short*)(scratch + vertex_count);
  177. for (size_t i = 0; i < vertex_count; ++i)
  178. destination[i] = unsigned(i);
  179. unsigned int* order[] = {scratch, destination};
  180. // 5-pass radix sort computes the resulting order into scratch
  181. for (int k = 0; k < 5; ++k)
  182. {
  183. // copy 10-bit key segments into keyk to reduce cache pressure during radix pass
  184. for (size_t i = 0; i < vertex_count; ++i)
  185. keyk[i] = (unsigned short)((keys[i] >> (k * 10)) & 1023);
  186. radixSort10(order[k % 2], order[(k + 1) % 2], keyk, vertex_count);
  187. }
  188. // since our remap table is mapping old=>new, we need to reverse it
  189. for (size_t i = 0; i < vertex_count; ++i)
  190. destination[scratch[i]] = unsigned(i);
  191. }
  192. void meshopt_spatialSortTriangles(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  193. {
  194. using namespace meshopt;
  195. assert(index_count % 3 == 0);
  196. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  197. assert(vertex_positions_stride % sizeof(float) == 0);
  198. (void)vertex_count;
  199. size_t face_count = index_count / 3;
  200. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  201. meshopt_Allocator allocator;
  202. float* centroids = allocator.allocate<float>(face_count * 3);
  203. for (size_t i = 0; i < face_count; ++i)
  204. {
  205. unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
  206. assert(a < vertex_count && b < vertex_count && c < vertex_count);
  207. const float* va = vertex_positions + a * vertex_stride_float;
  208. const float* vb = vertex_positions + b * vertex_stride_float;
  209. const float* vc = vertex_positions + c * vertex_stride_float;
  210. centroids[i * 3 + 0] = (va[0] + vb[0] + vc[0]) / 3.f;
  211. centroids[i * 3 + 1] = (va[1] + vb[1] + vc[1]) / 3.f;
  212. centroids[i * 3 + 2] = (va[2] + vb[2] + vc[2]) / 3.f;
  213. }
  214. unsigned int* remap = allocator.allocate<unsigned int>(face_count);
  215. meshopt_spatialSortRemap(remap, centroids, face_count, sizeof(float) * 3);
  216. // support in-order remap
  217. if (destination == indices)
  218. {
  219. unsigned int* indices_copy = allocator.allocate<unsigned int>(index_count);
  220. memcpy(indices_copy, indices, index_count * sizeof(unsigned int));
  221. indices = indices_copy;
  222. }
  223. for (size_t i = 0; i < face_count; ++i)
  224. {
  225. unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
  226. unsigned int r = remap[i];
  227. destination[r * 3 + 0] = a;
  228. destination[r * 3 + 1] = b;
  229. destination[r * 3 + 2] = c;
  230. }
  231. }
  232. void meshopt_spatialClusterPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t cluster_size)
  233. {
  234. using namespace meshopt;
  235. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  236. assert(vertex_positions_stride % sizeof(float) == 0);
  237. assert(cluster_size > 0);
  238. meshopt_Allocator allocator;
  239. unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);
  240. computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ false);
  241. unsigned int* order = allocator.allocate<unsigned int>(vertex_count * 3);
  242. unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 1b for side or 2b for keys
  243. unsigned short* keyk = reinterpret_cast<unsigned short*>(scratch + vertex_count);
  244. for (int k = 0; k < 3; ++k)
  245. {
  246. // copy 16-bit key segments into keyk to reduce cache pressure during radix pass
  247. for (size_t i = 0; i < vertex_count; ++i)
  248. keyk[i] = (unsigned short)(keys[i] >> (k * 20));
  249. unsigned int hist[256][2];
  250. computeHistogram(hist, keyk, vertex_count);
  251. for (size_t i = 0; i < vertex_count; ++i)
  252. order[k * vertex_count + i] = unsigned(i);
  253. radixPass(scratch, order + k * vertex_count, keyk, vertex_count, hist, 0);
  254. radixPass(order + k * vertex_count, scratch, keyk, vertex_count, hist, 1);
  255. }
  256. splitPoints(destination, order, order + vertex_count, order + 2 * vertex_count, keys, vertex_count, scratch, cluster_size);
  257. }