tr_orderIndexes.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. /*
  24. ===============
  25. R_MeshCost
  26. ===============
  27. */
  28. #define CACHE_SIZE 24
  29. #define STALL_SIZE 8
  30. int R_MeshCost( int numIndexes, glIndex_t *indexes ) {
  31. int inCache[CACHE_SIZE];
  32. int i, j, v;
  33. int c_stalls;
  34. int c_loads;
  35. int fifo;
  36. for ( i = 0 ; i < CACHE_SIZE ; i++ ) {
  37. inCache[i] = -1;
  38. }
  39. c_loads = 0;
  40. c_stalls = 0;
  41. fifo = 0;
  42. for ( i = 0 ; i < numIndexes ; i++ ) {
  43. v = indexes[i];
  44. for ( j = 0 ; j < CACHE_SIZE ; j++ ) {
  45. if ( inCache[ ( fifo + j ) % CACHE_SIZE ] == v ) {
  46. break;
  47. }
  48. }
  49. if ( j == CACHE_SIZE ) {
  50. c_loads++;
  51. inCache[ fifo % CACHE_SIZE ] = v;
  52. fifo++;
  53. } else if ( j < STALL_SIZE ) {
  54. c_stalls++;
  55. }
  56. }
  57. return c_loads;
  58. }
  59. typedef struct vertRef_s {
  60. struct vertRef_s *next;
  61. int tri;
  62. } vertRef_t;
  63. /*
  64. ====================
  65. R_OrderIndexes
  66. Reorganizes the indexes so they will take best advantage
  67. of the internal GPU vertex caches
  68. ====================
  69. */
  70. void R_OrderIndexes( int numIndexes, glIndex_t *indexes ) {
  71. bool *triangleUsed;
  72. int numTris;
  73. glIndex_t *oldIndexes;
  74. glIndex_t *base;
  75. int numOldIndexes;
  76. int tri;
  77. int i;
  78. vertRef_t *vref, **vrefs, *vrefTable;
  79. int numVerts;
  80. int v1, v2;
  81. int c_starts;
  82. int c_cost;
  83. if ( !r_orderIndexes.GetBool() ) {
  84. return;
  85. }
  86. // save off the original indexes
  87. oldIndexes = (glIndex_t *)_alloca( numIndexes * sizeof( *oldIndexes ) );
  88. memcpy( oldIndexes, indexes, numIndexes * sizeof( *oldIndexes ) );
  89. numOldIndexes = numIndexes;
  90. // make a table to mark the triangles when they are emited
  91. numTris = numIndexes / 3;
  92. triangleUsed = (bool *)_alloca( numTris * sizeof( *triangleUsed ) );
  93. memset( triangleUsed, 0, numTris * sizeof( *triangleUsed ) );
  94. // find the highest vertex number
  95. numVerts = 0;
  96. for ( i = 0 ; i < numIndexes ; i++ ) {
  97. if ( indexes[i] > numVerts ) {
  98. numVerts = indexes[i];
  99. }
  100. }
  101. numVerts++;
  102. // create a table of triangles used by each vertex
  103. vrefs = (vertRef_t **)_alloca( numVerts * sizeof( *vrefs ) );
  104. memset( vrefs, 0, numVerts * sizeof( *vrefs ) );
  105. vrefTable = (vertRef_t *)_alloca( numIndexes * sizeof( *vrefTable ) );
  106. for ( i = 0 ; i < numIndexes ; i++ ) {
  107. tri = i / 3;
  108. vrefTable[i].tri = tri;
  109. vrefTable[i].next = vrefs[oldIndexes[i]];
  110. vrefs[oldIndexes[i]] = &vrefTable[i];
  111. }
  112. // generate new indexes
  113. numIndexes = 0;
  114. c_starts = 0;
  115. while ( numIndexes != numOldIndexes ) {
  116. // find a triangle that hasn't been used
  117. for ( tri = 0 ; tri < numTris ; tri++ ) {
  118. if ( !triangleUsed[tri] ) {
  119. break;
  120. }
  121. }
  122. if ( tri == numTris ) {
  123. common->Error( "R_OrderIndexes: ran out of unused tris" );
  124. }
  125. c_starts++;
  126. do {
  127. // emit this tri
  128. base = oldIndexes + tri * 3;
  129. indexes[numIndexes+0] = base[0];
  130. indexes[numIndexes+1] = base[1];
  131. indexes[numIndexes+2] = base[2];
  132. numIndexes += 3;
  133. triangleUsed[tri] = true;
  134. // try to find a shared edge to another unused tri
  135. for ( i = 0 ; i < 3 ; i++ ) {
  136. v1 = base[i];
  137. v2 = base[(i+1)%3];
  138. for ( vref = vrefs[v1] ; vref ; vref = vref->next ) {
  139. tri = vref->tri;
  140. if ( triangleUsed[tri] ) {
  141. continue;
  142. }
  143. // if this triangle also uses v2, grab it
  144. if ( oldIndexes[tri*3+0] == v2
  145. || oldIndexes[tri*3+1] == v2
  146. || oldIndexes[tri*3+2] == v2 ) {
  147. break;
  148. }
  149. }
  150. if ( vref ) {
  151. break;
  152. }
  153. }
  154. // if we couldn't chain off of any verts, we need to find a new one
  155. if ( i == 3 ) {
  156. break;
  157. }
  158. } while ( 1 );
  159. }
  160. c_cost = R_MeshCost( numIndexes, indexes );
  161. }
  162. /*
  163. add all triangles that can be specified by the vertexes in the last 14 cache positions
  164. pick a new vert to add to the cache
  165. don't pick one in the 24 previous cache positions
  166. try to pick one that will enable the creation of as many triangles as possible
  167. look for a vert that shares an edge with the vert about to be evicted
  168. */