Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
4.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <windows.h>
  8. #include "HardwareMatrixState.h"
  9. #include <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "studio.h"
  13. #include "studiomdl.h"
  14. CHardwareMatrixState::CHardwareMatrixState()
  15. {
  16. m_LRUCounter = 0;
  17. m_NumMatrices = 0;
  18. m_matrixState = NULL;
  19. m_savedMatrixState = NULL;
  20. }
  21. void CHardwareMatrixState::Init( int numHardwareMatrices )
  22. {
  23. m_NumMatrices = numHardwareMatrices;
  24. delete [] m_matrixState;
  25. m_matrixState = new MatrixState_t[m_NumMatrices];
  26. Assert( m_matrixState );
  27. delete [] m_savedMatrixState;
  28. m_savedMatrixState = new MatrixState_t[m_NumMatrices];
  29. Assert( m_savedMatrixState );
  30. m_LRUCounter = 0;
  31. m_AllocatedMatrices = 0;
  32. int i;
  33. for( i = 0; i < m_NumMatrices; i++ )
  34. {
  35. m_matrixState[i].allocated = false;
  36. }
  37. }
  38. bool CHardwareMatrixState::AllocateMatrix( int globalMatrixID )
  39. {
  40. int i;
  41. if( IsMatrixAllocated( globalMatrixID ) )
  42. {
  43. return true;
  44. }
  45. for( i = 0; i < m_NumMatrices; i++ )
  46. {
  47. if( !m_matrixState[i].allocated )
  48. {
  49. m_matrixState[i].globalMatrixID = globalMatrixID;
  50. m_matrixState[i].allocated = true;
  51. m_matrixState[i].lastUsageID = m_LRUCounter++;
  52. ++m_AllocatedMatrices;
  53. DumpState();
  54. return true;
  55. }
  56. }
  57. DumpState();
  58. return false;
  59. }
  60. int CHardwareMatrixState::FindLocalLRUIndex( void )
  61. {
  62. int oldestLRUCounter = INT_MAX;
  63. int i;
  64. int oldestID = 0;
  65. for( i = 0; i < m_NumMatrices; i++ )
  66. {
  67. if( !m_matrixState[i].allocated )
  68. {
  69. continue;
  70. }
  71. if( m_matrixState[i].lastUsageID < oldestLRUCounter )
  72. {
  73. oldestLRUCounter = m_matrixState[i].lastUsageID;
  74. oldestID = i;
  75. }
  76. }
  77. Assert( oldestLRUCounter != INT_MAX );
  78. return oldestID;
  79. }
  80. void CHardwareMatrixState::DeallocateLRU( void )
  81. {
  82. int id;
  83. id = FindLocalLRUIndex();
  84. m_matrixState[id].allocated = false;
  85. --m_AllocatedMatrices;
  86. }
  87. void CHardwareMatrixState::DeallocateLRU( int n )
  88. {
  89. int i;
  90. for( i = 0; i < n; i++ )
  91. {
  92. DeallocateLRU();
  93. }
  94. }
  95. bool CHardwareMatrixState::IsMatrixAllocated( int globalMatrixID ) const
  96. {
  97. int i;
  98. for( i = 0; i < m_NumMatrices; i++ )
  99. {
  100. if( m_matrixState[i].globalMatrixID == globalMatrixID &&
  101. m_matrixState[i].allocated )
  102. {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. void CHardwareMatrixState::DeallocateAll()
  109. {
  110. int i;
  111. DumpState();
  112. for( i = 0; i < m_NumMatrices; i++ )
  113. {
  114. m_matrixState[i].allocated = false;
  115. m_matrixState[i].globalMatrixID = INT_MAX;
  116. m_matrixState[i].lastUsageID = INT_MAX;
  117. }
  118. m_AllocatedMatrices = 0;
  119. DumpState();
  120. }
  121. void CHardwareMatrixState::SaveState( void )
  122. {
  123. int i;
  124. for( i = 0; i < m_NumMatrices; i++ )
  125. {
  126. m_savedMatrixState[i] = m_matrixState[i];
  127. }
  128. }
  129. void CHardwareMatrixState::RestoreState( void )
  130. {
  131. int i;
  132. for( i = 0; i < m_NumMatrices; i++ )
  133. {
  134. m_matrixState[i] = m_savedMatrixState[i];
  135. }
  136. }
  137. int CHardwareMatrixState::AllocatedMatrixCount() const
  138. {
  139. return m_AllocatedMatrices;
  140. }
  141. int CHardwareMatrixState::FreeMatrixCount() const
  142. {
  143. return m_NumMatrices - m_AllocatedMatrices;
  144. }
  145. int CHardwareMatrixState::GetNthBoneGlobalID( int n ) const
  146. {
  147. int i;
  148. int m = 0;
  149. for( i = 0; i < m_NumMatrices; i++ )
  150. {
  151. if( m_matrixState[i].allocated )
  152. {
  153. if( n == m )
  154. {
  155. return m_matrixState[i].globalMatrixID;
  156. }
  157. m++;
  158. }
  159. }
  160. Assert( 0 );
  161. MdlError( "GetNthBoneGlobalID() Failure\n" );
  162. return 0;
  163. }
  164. void CHardwareMatrixState::DumpState( void )
  165. {
  166. int i;
  167. static char buf[256];
  168. //#ifndef _DEBUG
  169. return;
  170. //#endif
  171. OutputDebugString( "DumpState\n:" );
  172. for( i = 0; i < m_NumMatrices; i++ )
  173. {
  174. if( m_matrixState[i].allocated )
  175. {
  176. sprintf( buf, "%d: allocated: %s lastUsageID: %d globalMatrixID: %d\n",
  177. i,
  178. m_matrixState[i].allocated ? "true " : "false",
  179. m_matrixState[i].lastUsageID,
  180. m_matrixState[i].globalMatrixID );
  181. OutputDebugString( buf );
  182. }
  183. }
  184. }
  185. int CHardwareMatrixState::FindHardwareMatrix( int globalMatrixID )
  186. {
  187. int i;
  188. for( i = 0; i < m_NumMatrices; i++ )
  189. {
  190. if( m_matrixState[i].globalMatrixID == globalMatrixID )
  191. {
  192. return i;
  193. }
  194. }
  195. Assert( 0 );
  196. MdlError( "barfing in FindHardwareMatrix\n" );
  197. return 0;
  198. }