vertex_cache_optimizer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**************************************************************************/
  2. /* vertex_cache_optimizer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "vertex_cache_optimizer.h"
  31. #include "core/math/math_funcs.h"
  32. // Precalculate the tables.
  33. void VertexCacheOptimizer::init() {
  34. for (int i = 0; i < Constants::CACHE_SCORE_TABLE_SIZE; i++) {
  35. float score = 0;
  36. if (i < 3) {
  37. // This vertex was used in the last triangle,
  38. // so it has a fixed score, which ever of the three
  39. // it's in. Otherwise, you can get very different
  40. // answers depending on whether you add
  41. // the triangle 1,2,3 or 3,1,2 - which is silly.
  42. score = Constants::LAST_TRI_SCORE;
  43. } else {
  44. // Points for being high in the cache.
  45. const float scaler = 1.0f / (Constants::CACHE_FUNCTION_LENGTH - 3);
  46. score = 1.0f - (i - 3) * scaler;
  47. score = Math::pow(score, Constants::CACHE_DECAY_POWER);
  48. }
  49. _cache_position_score[i] = (SCORE_TYPE)(Constants::SCORE_SCALING * score);
  50. }
  51. for (int i = 1; i < Constants::VALENCE_SCORE_TABLE_SIZE; i++) {
  52. // Bonus points for having a low number of tris still to
  53. // use the vert, so we get rid of lone verts quickly.
  54. float valence_boost = Math::pow(i, -Constants::VALENCE_BOOST_POWER);
  55. float score = Constants::VALENCE_BOOST_SCALE * valence_boost;
  56. _valence_score[i] = (SCORE_TYPE)(Constants::SCORE_SCALING * score);
  57. }
  58. }
  59. VertexCacheOptimizer::SCORE_TYPE VertexCacheOptimizer::find_vertex_score(int p_num_active_tris, int p_cache_position) {
  60. if (p_num_active_tris == 0) {
  61. // No triangles need this vertex!
  62. return 0;
  63. }
  64. SCORE_TYPE score = 0;
  65. if (p_cache_position < 0) {
  66. // Vertex is not in LRU cache - no score.
  67. } else {
  68. score = _cache_position_score[p_cache_position];
  69. }
  70. if (p_num_active_tris < Constants::VALENCE_SCORE_TABLE_SIZE) {
  71. score += _valence_score[p_num_active_tris];
  72. }
  73. return score;
  74. }
  75. VertexCacheOptimizer::VERTEX_INDEX_TYPE *VertexCacheOptimizer::_reorder_indices(VERTEX_INDEX_TYPE *r_dest_indices, const VERTEX_INDEX_TYPE *p_source_indices, int p_num_triangles, int p_num_vertices) {
  76. ADJACENCY_TYPE *num_active_tris = (ADJACENCY_TYPE *)memalloc(sizeof(ADJACENCY_TYPE) * p_num_vertices);
  77. memset(num_active_tris, 0, sizeof(ADJACENCY_TYPE) * p_num_vertices);
  78. // First scan over the vertex data, count the total number of
  79. // occurrances of each vertex.
  80. for (int i = 0; i < 3 * p_num_triangles; i++) {
  81. if (num_active_tris[p_source_indices[i]] == Constants::MAX_ADJACENCY) {
  82. // Unsupported mesh,
  83. // vertex shared by too many triangles.
  84. memfree(num_active_tris);
  85. return nullptr;
  86. }
  87. num_active_tris[p_source_indices[i]]++;
  88. }
  89. // Allocate the rest of the arrays.
  90. ARRAY_INDEX_TYPE *offsets = (ARRAY_INDEX_TYPE *)memalloc(sizeof(ARRAY_INDEX_TYPE) * p_num_vertices);
  91. SCORE_TYPE *last_score = (SCORE_TYPE *)memalloc(sizeof(SCORE_TYPE) * p_num_vertices);
  92. CACHE_POS_TYPE *cache_tag = (CACHE_POS_TYPE *)memalloc(sizeof(CACHE_POS_TYPE) * p_num_vertices);
  93. uint8_t *triangle_added = (uint8_t *)memalloc((p_num_triangles + 7) / 8);
  94. SCORE_TYPE *triangle_score = (SCORE_TYPE *)memalloc(sizeof(SCORE_TYPE) * p_num_triangles);
  95. TRIANGLE_INDEX_TYPE *triangle_indices = (TRIANGLE_INDEX_TYPE *)memalloc(sizeof(TRIANGLE_INDEX_TYPE) * 3 * p_num_triangles);
  96. memset(triangle_added, 0, sizeof(uint8_t) * ((p_num_triangles + 7) / 8));
  97. memset(triangle_score, 0, sizeof(SCORE_TYPE) * p_num_triangles);
  98. memset(triangle_indices, 0, sizeof(TRIANGLE_INDEX_TYPE) * 3 * p_num_triangles);
  99. // Count the triangle array offset for each vertex,
  100. // initialize the rest of the data.
  101. int sum = 0;
  102. for (int i = 0; i < p_num_vertices; i++) {
  103. offsets[i] = sum;
  104. sum += num_active_tris[i];
  105. num_active_tris[i] = 0;
  106. cache_tag[i] = -1;
  107. }
  108. // Fill the vertex data structures with indices to the triangles
  109. // using each vertex.
  110. for (int i = 0; i < p_num_triangles; i++) {
  111. for (int j = 0; j < 3; j++) {
  112. int v = p_source_indices[3 * i + j];
  113. triangle_indices[offsets[v] + num_active_tris[v]] = i;
  114. num_active_tris[v]++;
  115. }
  116. }
  117. // Initialize the score for all vertices.
  118. for (int i = 0; i < p_num_vertices; i++) {
  119. last_score[i] = find_vertex_score(num_active_tris[i], cache_tag[i]);
  120. for (int j = 0; j < num_active_tris[i]; j++) {
  121. triangle_score[triangle_indices[offsets[i] + j]] += last_score[i];
  122. }
  123. }
  124. // Find the best triangle.
  125. int best_triangle = -1;
  126. int best_score = -1;
  127. for (int i = 0; i < p_num_triangles; i++) {
  128. if (triangle_score[i] > best_score) {
  129. best_score = triangle_score[i];
  130. best_triangle = i;
  131. }
  132. }
  133. // Allocate the output array.
  134. TRIANGLE_INDEX_TYPE *out_triangles = (TRIANGLE_INDEX_TYPE *)memalloc(sizeof(TRIANGLE_INDEX_TYPE) * p_num_triangles);
  135. int out_pos = 0;
  136. // Initialize the cache.
  137. int cache[Constants::VERTEX_CACHE_SIZE + 3];
  138. for (int i = 0; i < Constants::VERTEX_CACHE_SIZE + 3; i++) {
  139. cache[i] = -1;
  140. }
  141. int scan_pos = 0;
  142. // Output the currently best triangle, as long as there
  143. // are triangles left to output.
  144. while (best_triangle >= 0) {
  145. // Mark the triangle as added.
  146. set_added(triangle_added, best_triangle);
  147. // Output this triangle.
  148. out_triangles[out_pos++] = best_triangle;
  149. for (int i = 0; i < 3; i++) {
  150. // Update this vertex.
  151. int v = p_source_indices[3 * best_triangle + i];
  152. // Check the current cache position, if it
  153. // is in the cache.
  154. int endpos = cache_tag[v];
  155. if (endpos < 0) {
  156. endpos = Constants::VERTEX_CACHE_SIZE + i;
  157. }
  158. if (endpos > i) {
  159. // Move all cache entries from the previous position
  160. // in the cache to the new target position (i) one
  161. // step backwards.
  162. for (int j = endpos; j > i; j--) {
  163. cache[j] = cache[j - 1];
  164. // If this cache slot contains a real
  165. // vertex, update its cache tag.
  166. if (cache[j] >= 0) {
  167. cache_tag[cache[j]]++;
  168. }
  169. }
  170. // Insert the current vertex into its new target
  171. // slot.
  172. cache[i] = v;
  173. cache_tag[v] = i;
  174. }
  175. // Find the current triangle in the list of active
  176. // triangles and remove it (moving the last
  177. // triangle in the list to the slot of this triangle).
  178. for (int j = 0; j < num_active_tris[v]; j++) {
  179. if (triangle_indices[offsets[v] + j] == best_triangle) {
  180. triangle_indices[offsets[v] + j] = triangle_indices[offsets[v] + num_active_tris[v] - 1];
  181. break;
  182. }
  183. }
  184. // Shorten the list.
  185. num_active_tris[v]--;
  186. }
  187. // Update the scores of all triangles in the cache.
  188. for (int i = 0; i < Constants::VERTEX_CACHE_SIZE + 3; i++) {
  189. int v = cache[i];
  190. if (v < 0) {
  191. break;
  192. }
  193. // This vertex has been pushed outside of the
  194. // actual cache.
  195. if (i >= Constants::VERTEX_CACHE_SIZE) {
  196. cache_tag[v] = -1;
  197. cache[i] = -1;
  198. }
  199. SCORE_TYPE newScore = find_vertex_score(num_active_tris[v], cache_tag[v]);
  200. SCORE_TYPE diff = newScore - last_score[v];
  201. for (int j = 0; j < num_active_tris[v]; j++) {
  202. triangle_score[triangle_indices[offsets[v] + j]] += diff;
  203. }
  204. last_score[v] = newScore;
  205. }
  206. // Find the best triangle referenced by vertices in the cache.
  207. best_triangle = -1;
  208. best_score = -1;
  209. for (int i = 0; i < Constants::VERTEX_CACHE_SIZE; i++) {
  210. if (cache[i] < 0) {
  211. break;
  212. }
  213. int v = cache[i];
  214. for (int j = 0; j < num_active_tris[v]; j++) {
  215. int t = triangle_indices[offsets[v] + j];
  216. if (triangle_score[t] > best_score) {
  217. best_triangle = t;
  218. best_score = triangle_score[t];
  219. }
  220. }
  221. }
  222. // If no active triangle was found at all, continue
  223. // scanning the whole list of triangles.
  224. if (best_triangle < 0) {
  225. for (; scan_pos < p_num_triangles; scan_pos++) {
  226. if (!is_added(triangle_added, scan_pos)) {
  227. best_triangle = scan_pos;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. // Convert the triangle index array into a full triangle list.
  234. out_pos = 0;
  235. for (int i = 0; i < p_num_triangles; i++) {
  236. int t = out_triangles[i];
  237. for (int j = 0; j < 3; j++) {
  238. int v = p_source_indices[3 * t + j];
  239. r_dest_indices[out_pos++] = v;
  240. }
  241. }
  242. // Clean up.
  243. memfree(triangle_indices);
  244. memfree(offsets);
  245. memfree(last_score);
  246. memfree(num_active_tris);
  247. memfree(cache_tag);
  248. memfree(triangle_added);
  249. memfree(triangle_score);
  250. memfree(out_triangles);
  251. return r_dest_indices;
  252. }
  253. bool VertexCacheOptimizer::reorder_indices_pool(PoolVector<int> &r_indices, uint32_t p_num_triangles, uint32_t p_num_verts) {
  254. LocalVector<int> temp;
  255. temp = r_indices;
  256. if (reorder_indices(temp, p_num_triangles, p_num_verts)) {
  257. r_indices = temp;
  258. return true;
  259. }
  260. return false;
  261. }
  262. bool VertexCacheOptimizer::reorder_indices(LocalVector<int> &r_indices, uint32_t p_num_triangles, uint32_t p_num_verts) {
  263. LocalVector<int> temp;
  264. temp.resize(r_indices.size());
  265. if (_reorder_indices((VERTEX_INDEX_TYPE *)temp.ptr(), (VERTEX_INDEX_TYPE *)r_indices.ptr(), p_num_triangles, p_num_verts)) {
  266. #if 0
  267. uint32_t show = MIN(r_indices.size(), 16);
  268. for (uint32_t n = 0; n < show; n++) {
  269. print_line(itos(n) + " : " + itos(r_indices[n]) + " to " + itos(temp[n]));
  270. }
  271. #endif
  272. r_indices = temp;
  273. return true;
  274. }
  275. return false;
  276. }