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.

264 lines
7.1 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef BVH_SUPPORT_H
  9. #define BVH_SUPPORT_H
  10. #include <tier0/platform.h>
  11. #include <mathlib/vector.h>
  12. #include <mathlib/vector4d.h>
  13. #include <mathlib/mathlib.h>
  14. #include <mathlib/vmatrix.h>
  15. //--------------------------------------------------------------------------------------
  16. // Temp material: this is all throwaway
  17. //--------------------------------------------------------------------------------------
  18. #define MAX_SHADER_NAME 48
  19. #define MAX_BINDS 16
  20. enum BindStage_t
  21. {
  22. STAGE_VS = 0,
  23. STAGE_HS = 1,
  24. STAGE_DS = 2,
  25. STAGE_GS = 3,
  26. STAGE_PS = 4
  27. };
  28. enum BindSampler_t
  29. {
  30. SAMPLER_LINEAR = 0,
  31. SAMPLER_POINT = 1,
  32. SAMPLER_ANISO = 2,
  33. SAMPLER_SHADOW_LESS = 3
  34. };
  35. struct MaterialResourceBinding_t
  36. {
  37. DECLARE_BYTESWAP_DATADESC();
  38. uint8 m_cBindStage; // Which stage to bind to (VS,HS,DS,GS,PS)
  39. uint8 m_cBindSlot; // Which slot to bind to in this stage
  40. uint8 m_cBindSampler; // If this is a texture, this is the sampler enum to use
  41. //uint8 m_padding; // don't worry about padding, because we're storing an array of 16 of them
  42. };
  43. struct Material_t
  44. {
  45. DECLARE_BYTESWAP_DATADESC();
  46. char m_szShaderVS[MAX_SHADER_NAME];
  47. char m_szShaderPS[MAX_SHADER_NAME];
  48. int32 m_nBinds;
  49. MaterialResourceBinding_t m_Binds[MAX_BINDS];
  50. float m_flPhongExp;
  51. float m_flPhongBoost;
  52. // Alloc these in blocks of 4 for alignment
  53. bool m_bAlphaTest; // Need to explicitly call out alpha-test so that we can bind textures during the shadow pass
  54. bool m_bInstanced; // use instancing for this material
  55. bool m_bUseAtlas; // use an atlas with an indirection texture
  56. bool m_bVertexColor; // don't use a texture map, color is in the vertices
  57. bool m_bNormalMap; // have a normal map
  58. bool m_bPhong; // use phong
  59. bool m_bPhongTexture; // pull phong exp from a texture
  60. bool m_bPadding;
  61. };
  62. //--------------------------------------------------------------------------------------
  63. // Tiled coordinate
  64. //--------------------------------------------------------------------------------------
  65. struct IntVector
  66. {
  67. DECLARE_BYTESWAP_DATADESC();
  68. int x,y,z;
  69. };
  70. extern Vector g_vWorldUnitsPerTile;
  71. struct TiledPosition_t
  72. {
  73. DECLARE_BYTESWAP_DATADESC();
  74. IntVector m_vTile;
  75. Vector m_vLocal;
  76. void Rationalize()
  77. {
  78. Vector vFloor( floor( m_vLocal.x / g_vWorldUnitsPerTile.x ),
  79. floor( m_vLocal.y / g_vWorldUnitsPerTile.y ),
  80. floor( m_vLocal.z / g_vWorldUnitsPerTile.z ) );
  81. m_vTile.x += (int)vFloor.x;
  82. m_vTile.y += (int)vFloor.y;
  83. m_vTile.z += (int)vFloor.z;
  84. m_vLocal -= vFloor * g_vWorldUnitsPerTile;
  85. }
  86. TiledPosition_t operator +( TiledPosition_t &Other )
  87. {
  88. TiledPosition_t retVal;
  89. retVal.m_vTile.x = m_vTile.x + Other.m_vTile.x;
  90. retVal.m_vTile.y = m_vTile.y + Other.m_vTile.y;
  91. retVal.m_vTile.z = m_vTile.z + Other.m_vTile.z;
  92. retVal.m_vLocal = m_vLocal + Other.m_vLocal;
  93. retVal.Rationalize();
  94. return retVal;
  95. }
  96. TiledPosition_t operator -( TiledPosition_t &Other )
  97. {
  98. TiledPosition_t retVal;
  99. retVal.m_vTile.x = m_vTile.x - Other.m_vTile.x;
  100. retVal.m_vTile.y = m_vTile.y - Other.m_vTile.y;
  101. retVal.m_vTile.z = m_vTile.z - Other.m_vTile.z;
  102. retVal.m_vLocal = m_vLocal - Other.m_vLocal;
  103. retVal.Rationalize();
  104. return retVal;
  105. }
  106. Vector ToWorldPosition()
  107. {
  108. Vector vTile( m_vTile.x, m_vTile.y, m_vTile.z );
  109. return vTile * g_vWorldUnitsPerTile + m_vLocal;
  110. }
  111. };
  112. #include "frustum.h"
  113. //--------------------------------------------------------------------------------------
  114. // generic buffer for variable sized vertices
  115. //--------------------------------------------------------------------------------------
  116. class CGenericBuffer
  117. {
  118. public:
  119. CGenericBuffer( int nElementStride = 0, int nGrowElements = 1024, int nInitElements = 1024 ) :
  120. m_pMemory( NULL ),
  121. m_nAllocBytes( 0 )
  122. {
  123. Init( nElementStride, nGrowElements, nInitElements );
  124. }
  125. ~CGenericBuffer()
  126. {
  127. Purge();
  128. }
  129. void Init( int nElementStride, int nGrowElements = 1024, int nInitElements = 1024 )
  130. {
  131. Purge();
  132. m_nElementStride = nElementStride;
  133. m_nGrowBytes = nGrowElements * nElementStride;
  134. m_nAllocBytes = nInitElements * nElementStride;
  135. m_pMemory = new unsigned char[ nInitElements * nElementStride ];
  136. }
  137. int GetElementStride() const { return m_nElementStride; }
  138. unsigned char *Base() const { return m_pMemory; }
  139. int ByteCount() const { return m_nElements * m_nElementStride; }
  140. int Count() const { return m_nElements; }
  141. int AddToTail( unsigned char* pElement )
  142. {
  143. EnsureAddSize( 1 );
  144. Q_memcpy( m_pMemory + m_nElements * m_nElementStride, pElement, m_nElementStride );
  145. m_nElements++;
  146. return m_nElements-1;
  147. }
  148. int AddMultipleToTail( int nCount, unsigned char* pElements )
  149. {
  150. EnsureAddSize( nCount );
  151. Q_memcpy( m_pMemory + m_nElements * m_nElementStride, pElements, nCount * m_nElementStride );
  152. m_nElements += nCount;
  153. return m_nElements-1;
  154. }
  155. int AddMultipleToTail( int nCount )
  156. {
  157. EnsureAddSize( nCount );
  158. Q_memset( m_pMemory + m_nElements * m_nElementStride, 0, nCount * m_nElementStride );
  159. m_nElements += nCount;
  160. return m_nElements-1;
  161. }
  162. void RestrideElements( int nNewStride )
  163. {
  164. uint8 *pNewMemory = new uint8[ nNewStride * m_nElements ];
  165. int nMinStride = MIN( nNewStride, m_nElementStride );
  166. uint8 *pNewStart = pNewMemory;
  167. for ( int i=0; i<m_nElements; ++i )
  168. {
  169. Q_memcpy( pNewStart, m_pMemory + i * m_nElementStride, nMinStride );
  170. pNewStart += nNewStride;
  171. }
  172. delete []m_pMemory;
  173. m_pMemory = pNewMemory;
  174. m_nElementStride = nNewStride;
  175. m_nAllocBytes = nNewStride * m_nElements;
  176. }
  177. void RemoveAll()
  178. {
  179. m_nElements = 0;
  180. }
  181. void Purge()
  182. {
  183. if ( m_pMemory )
  184. delete []m_pMemory;
  185. m_pMemory = NULL;
  186. m_nElements = 0;
  187. m_nAllocBytes = 0;
  188. }
  189. unsigned char* operator[]( int i )
  190. {
  191. return m_pMemory + i * m_nElementStride;
  192. }
  193. private:
  194. void Resize( int nNeededBytes )
  195. {
  196. unsigned char *pNewMemory = new unsigned char[ nNeededBytes ];
  197. Q_memcpy( pNewMemory, m_pMemory, m_nAllocBytes );
  198. m_nAllocBytes = nNeededBytes;
  199. delete []m_pMemory;
  200. m_pMemory = pNewMemory;
  201. }
  202. inline void EnsureAddSize( int nElementsToAdd )
  203. {
  204. int nUsedBytes = m_nElements * m_nElementStride;
  205. int nNeededBytes = nUsedBytes + nElementsToAdd * m_nElementStride;
  206. if ( m_nAllocBytes < nNeededBytes )
  207. {
  208. Resize( MAX( nNeededBytes, nUsedBytes + m_nGrowBytes ) );
  209. }
  210. }
  211. private:
  212. unsigned char *m_pMemory;
  213. int m_nElementStride;
  214. int m_nElements;
  215. int m_nAllocBytes;
  216. int m_nGrowBytes;
  217. };
  218. //--------------------------------------------------------------------------------------
  219. //
  220. //--------------------------------------------------------------------------------------
  221. #endif