BoundsTrack.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #undef min // windef.h macros
  23. #undef max
  24. #include "BoundsTrack.h"
  25. /*
  26. We want to do one SIMD compare on 8 short components and know that the bounds
  27. overlap if all 8 tests pass
  28. */
  29. // shortBounds_t is used to track the reference bounds of all entities in a
  30. // cache-friendly and easy to compare way.
  31. //
  32. // To allow all elements to be compared with a single comparison sense, the maxs
  33. // are stored as negated values.
  34. //
  35. // We may need to add a global scale factor to this if there are intersections
  36. // completely outside +/-32k
  37. struct shortBounds_t {
  38. shortBounds_t() {
  39. SetToEmpty();
  40. }
  41. shortBounds_t( const idBounds & b ) {
  42. SetFromReferenceBounds( b );
  43. }
  44. short b[2][4]; // fourth element is just for padding
  45. idBounds ToFloatBounds() const {
  46. idBounds f;
  47. for ( int i = 0 ; i < 3 ; i++ ) {
  48. f[0][i] = b[0][i];
  49. f[1][i] = -b[1][i];
  50. }
  51. return f;
  52. }
  53. bool IntersectsShortBounds( shortBounds_t & comp ) const {
  54. shortBounds_t test;
  55. comp.MakeComparisonBounds( test );
  56. return IntersectsComparisonBounds( test );
  57. }
  58. bool IntersectsComparisonBounds( shortBounds_t & test ) const {
  59. // this can be a single ALTIVEC vcmpgtshR instruction
  60. return test.b[0][0] > b[0][0]
  61. && test.b[0][1] > b[0][1]
  62. && test.b[0][2] > b[0][2]
  63. && test.b[0][3] > b[0][3]
  64. && test.b[1][0] > b[1][0]
  65. && test.b[1][1] > b[1][1]
  66. && test.b[1][2] > b[1][2]
  67. && test.b[1][3] > b[1][3];
  68. }
  69. void MakeComparisonBounds( shortBounds_t & comp ) const {
  70. comp.b[0][0] = -b[1][0];
  71. comp.b[1][0] = -b[0][0];
  72. comp.b[0][1] = -b[1][1];
  73. comp.b[1][1] = -b[0][1];
  74. comp.b[0][2] = -b[1][2];
  75. comp.b[1][2] = -b[0][2];
  76. comp.b[0][3] = 0x7fff;
  77. comp.b[1][3] = 0x7fff;
  78. }
  79. void SetFromReferenceBounds( const idBounds & set ) {
  80. // the maxs are stored negated
  81. for ( int i = 0 ; i < 3 ; i++ ) {
  82. int minv = floor( set[0][i] );
  83. b[0][i] = std::max( -32768, minv );
  84. int maxv = -ceil( set[1][i] );
  85. b[1][i] = std::min( 32767, maxv );
  86. }
  87. b[0][3] = b[1][3] = 0;
  88. }
  89. void SetToEmpty() {
  90. // this will always fail the comparison
  91. for ( int i = 0 ; i < 2 ; i++ ) {
  92. for ( int j = 0 ; j < 4 ; j++ ) {
  93. b[i][j] = 0x7fff;
  94. }
  95. }
  96. }
  97. };
  98. // pure function
  99. int FindBoundsIntersectionsTEST(
  100. const shortBounds_t testBounds,
  101. const shortBounds_t * const boundsList,
  102. const int numBounds,
  103. int * const returnedList ) {
  104. int hits = 0;
  105. idBounds testF = testBounds.ToFloatBounds();
  106. for ( int i = 0 ; i < numBounds ; i++ ) {
  107. idBounds listF = boundsList[i].ToFloatBounds();
  108. if ( testF.IntersectsBounds( listF ) ) {
  109. returnedList[hits++] = i;
  110. }
  111. }
  112. return hits;
  113. }
  114. // pure function
  115. int FindBoundsIntersectionsSimSIMD(
  116. const shortBounds_t testBounds,
  117. const shortBounds_t * const boundsList,
  118. const int numBounds,
  119. int * const returnedList ) {
  120. shortBounds_t compareBounds;
  121. testBounds.MakeComparisonBounds( compareBounds );
  122. int hits = 0;
  123. for ( int i = 0 ; i < numBounds ; i++ ) {
  124. const shortBounds_t & listBounds = boundsList[i];
  125. bool compare[8];
  126. int count = 0;
  127. for ( int j = 0 ; j < 8 ; j++ ) {
  128. if ( ((short *)&compareBounds)[j] >= ((short *)&listBounds)[j] ) {
  129. compare[j] = true;
  130. count++;
  131. } else {
  132. compare[j] = false;
  133. }
  134. }
  135. if ( count == 8 ) {
  136. returnedList[hits++] = i;
  137. }
  138. }
  139. return hits;
  140. }
  141. idBoundsTrack::idBoundsTrack() {
  142. boundsList = (shortBounds_t *)Mem_Alloc( MAX_BOUNDS_TRACK_INDEXES * sizeof( *boundsList ), TAG_RENDER );
  143. ClearAll();
  144. }
  145. idBoundsTrack::~idBoundsTrack() {
  146. Mem_Free( boundsList );
  147. }
  148. void idBoundsTrack::ClearAll() {
  149. maxIndex = 0;
  150. for ( int i = 0 ; i < MAX_BOUNDS_TRACK_INDEXES ; i++ ) {
  151. ClearIndex( i );
  152. }
  153. }
  154. void idBoundsTrack::SetIndex( const int index, const idBounds & bounds ) {
  155. assert( (unsigned)index < MAX_BOUNDS_TRACK_INDEXES );
  156. maxIndex = std::max( maxIndex, index+1);
  157. boundsList[index].SetFromReferenceBounds( bounds );
  158. }
  159. void idBoundsTrack::ClearIndex( const int index ) {
  160. assert( (unsigned)index < MAX_BOUNDS_TRACK_INDEXES );
  161. boundsList[index].SetToEmpty();
  162. }
  163. int idBoundsTrack::FindIntersections( const idBounds & testBounds, int intersectedIndexes[ MAX_BOUNDS_TRACK_INDEXES ] ) const {
  164. const shortBounds_t shortTestBounds( testBounds );
  165. return FindBoundsIntersectionsTEST( shortTestBounds, boundsList, maxIndex, intersectedIndexes );
  166. }
  167. void idBoundsTrack::Test() {
  168. ClearAll();
  169. idRandom r;
  170. for ( int i = 0 ; i < 1800 ; i++ ) {
  171. idBounds b;
  172. for ( int j = 0 ; j < 3 ; j++ ) {
  173. b[0][j] = r.RandomInt( 20000 ) - 10000;
  174. b[1][j] = b[0][j] + r.RandomInt( 1000 );
  175. }
  176. SetIndex( i, b );
  177. }
  178. const idBounds testBounds( idVec3( -1000, 2000, -3000 ), idVec3( 1500, 4500, -500 ) );
  179. SetIndex( 1800, testBounds );
  180. SetIndex( 0, testBounds );
  181. const shortBounds_t shortTestBounds( testBounds );
  182. int intersectedIndexes1[ MAX_BOUNDS_TRACK_INDEXES ];
  183. const int numHits1 = FindBoundsIntersectionsTEST( shortTestBounds, boundsList, maxIndex, intersectedIndexes1 );
  184. int intersectedIndexes2[ MAX_BOUNDS_TRACK_INDEXES ];
  185. const int numHits2 = FindBoundsIntersectionsSimSIMD( shortTestBounds, boundsList, maxIndex, intersectedIndexes2 );
  186. idLib::Printf( "%i intersections\n", numHits1 );
  187. if ( numHits1 != numHits2 ) {
  188. idLib::Printf( "different results\n" );
  189. } else {
  190. for ( int i = 0 ; i < numHits1 ; i++ ) {
  191. if ( intersectedIndexes1[i] != intersectedIndexes2[i] ) {
  192. idLib::Printf( "different results\n" );
  193. break;
  194. }
  195. }
  196. }
  197. // run again for debugging failure
  198. FindBoundsIntersectionsTEST( shortTestBounds, boundsList, maxIndex, intersectedIndexes1 );
  199. FindBoundsIntersectionsSimSIMD( shortTestBounds, boundsList, maxIndex, intersectedIndexes2 );
  200. // timing
  201. const int64 start = Sys_Microseconds();
  202. for ( int i = 0 ; i < 40 ; i++ ) {
  203. FindBoundsIntersectionsSimSIMD( shortTestBounds, boundsList, maxIndex, intersectedIndexes2 );
  204. }
  205. const int64 stop = Sys_Microseconds();
  206. idLib::Printf( "%i microseconds for 40 itterations\n", stop - start );
  207. }
  208. class interactionPair_t {
  209. int entityIndex;
  210. int lightIndex;
  211. };
  212. /*
  213. keep a sorted list of static interactions and interactions already generated this frame?
  214. determine if the light needs more exact culling because it is rotated or a spot light
  215. for each entity on the bounds intersection list
  216. if entity is not directly visible, determine if it can cast a shadow into the view
  217. if the light center is in-frustum
  218. and the entity bounds is out-of-frustum, it can't contribue
  219. else the light center is off-frustum
  220. if any of the view frustum planes can be moved out to the light center and the entity bounds is still outside it, it can't contribute
  221. if a static interaction exists
  222. continue
  223. possibly perform more exact refernce bounds to rotated or spot light
  224. create an interaction pair and add it to the list
  225. all models will have an interaction with light -1 for ambient surface
  226. sort the interaction list by model
  227. do
  228. if the model is dynamic, create it
  229. add the ambient surface and skip interaction -1
  230. for all interactions
  231. check for static interaction
  232. check for current-frame interaction
  233. else create shadow for this light
  234. */