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.

304 lines
9.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. // wrapper for the material system for the engine.
  9. #ifndef GL_MATSYSIFACE_H
  10. #define GL_MATSYSIFACE_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "ivrenderview.h"
  15. #include "convar.h"
  16. #include "surfacehandle.h"
  17. #include "utlvector.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. class IMaterialSystemHardwareConfig;
  22. struct MaterialSystem_Config_t;
  23. class IMaterial;
  24. class IDebugTextureInfo;
  25. class Vector;
  26. struct mprimitive_t;
  27. class CMeshBuilder;
  28. struct model_t;
  29. //-----------------------------------------------------------------------------
  30. // global interfaces
  31. //-----------------------------------------------------------------------------
  32. extern const MaterialSystem_Config_t *g_pMaterialSystemConfig;
  33. extern MaterialSystem_SortInfo_t *materialSortInfoArray;
  34. extern bool g_LostVideoMemory;
  35. void MaterialSystem_DestroySortinfo( void );
  36. void MaterialSystem_CreateSortinfo( void );
  37. void MaterialSystem_RegisterPaintSurfaces( void );
  38. void InitMaterialSystem( void );
  39. void ShutdownMaterialSystem( void );
  40. void InitStartupScreen();
  41. void UpdateMaterialSystemConfig( void );
  42. bool MaterialConfigLightingChanged();
  43. void ClearMaterialConfigLightingChanged();
  44. void OverrideMaterialSystemConfig( MaterialSystem_Config_t &config );
  45. void MaterialSystem_RegisterLightmapSurfaces( void );
  46. IMaterial *GetMaterialAtCrossHair( void );
  47. bool SurfHasBumpedLightmaps( SurfaceHandle_t surfID );
  48. bool SurfNeedsBumpedLightmaps( SurfaceHandle_t surfID );
  49. bool SurfHasLightmap( SurfaceHandle_t surfID );
  50. bool SurfNeedsLightmap( SurfaceHandle_t surfID );
  51. void InitWellKnownRenderTargets( void );
  52. void ShutdownWellKnownRenderTargets( void );
  53. void InitMaterialSystemConfig( bool bInEditMode );
  54. #ifndef DEDICATED
  55. # ifdef NEWMESH
  56. extern CUtlVector<IVertexBuffer *> g_WorldStaticMeshes;
  57. # else
  58. extern CUtlVector<IMesh *> g_WorldStaticMeshes;
  59. # endif
  60. #endif
  61. extern CUtlVector<IMesh *> g_DepthMeshForSortID;
  62. struct materiallist_t
  63. {
  64. int nextBlock;
  65. int count;
  66. msurface2_t *pSurfaces[14];
  67. };
  68. struct surfacesortgroup_t
  69. {
  70. int32 listHead;
  71. int32 listTail;
  72. int32 groupListIndex;
  73. uint32 vertexCount;
  74. uint32 vertexCountNoDetail;
  75. uint32 indexCountNoDetail;
  76. uint32 triangleCount;
  77. uint32 surfaceCount;
  78. };
  79. class CMSurfaceSortList
  80. {
  81. public:
  82. void Init( int maxSortIDs, int minMaterialLists );
  83. void Shutdown();
  84. void Reset();
  85. void AddSurfaceToTail( msurface2_t *pSurface, int nSortGroup, int sortID );
  86. msurface2_t *GetSurfaceAtHead( const surfacesortgroup_t &group ) const;
  87. void GetSurfaceListForGroup( CUtlVector<msurface2_t *> &list, const surfacesortgroup_t &group ) const;
  88. inline int GetIndexForSortID( int nSortGroup, int sortID ) const
  89. {
  90. Assert(sortID<m_maxSortIDs);
  91. return groupOffset[nSortGroup] + sortID;
  92. }
  93. #ifdef _PS3
  94. void EnsureCapacityForSPU( int maxSortIDs, int minMaterialLists );
  95. inline const surfacesortgroup_t &GetGroupByIndex( int groupIndex ) const
  96. {
  97. if (!IsGroupUsed(groupIndex))
  98. return m_emptyGroup;
  99. return m_groupsShared[m_groupIndices[groupIndex]];
  100. }
  101. #else
  102. inline const surfacesortgroup_t &GetGroupByIndex( int groupIndex ) const
  103. {
  104. if (!IsGroupUsed(groupIndex))
  105. return m_emptyGroup;
  106. return m_groups[groupIndex];
  107. }
  108. #endif
  109. inline const CUtlVector<surfacesortgroup_t *> &GetSortList( int nSortGroup ) const
  110. {
  111. return m_sortGroupLists[nSortGroup];
  112. }
  113. inline const materiallist_t &GetSurfaceBlock(short index) const
  114. {
  115. return m_list[index];
  116. }
  117. #if defined(_PS3)
  118. inline const materiallist_t *GetMaterialList( void ) const
  119. {
  120. return &m_list[0];
  121. }
  122. inline const surfacesortgroup_t *GetGroupsShared( void ) const
  123. {
  124. return &m_groupsShared[0];
  125. }
  126. inline const uint16 *GetGroupIndices( void ) const
  127. {
  128. return &m_groupIndices[0];
  129. }
  130. inline surfacesortgroup_t **GetSortGroupLists( int nSortGroup )
  131. {
  132. return (m_sortGroupLists[ nSortGroup ].Base());
  133. }
  134. // base addresses of CUtlVecs for SPU
  135. inline void *GetMaterialListUtlPtr( void )
  136. {
  137. return &m_list;
  138. }
  139. inline void *GetGroupsSharedUtlPtr( void )
  140. {
  141. return &m_groupsShared;
  142. }
  143. inline void *GetGroupIndicesUtlPtr( void )
  144. {
  145. return &m_groupIndices;
  146. }
  147. inline void *GetSortGroupListsUtlPtr( int nSortGroup )
  148. {
  149. return &m_sortGroupLists[ nSortGroup ];
  150. }
  151. #endif
  152. inline const surfacesortgroup_t &GetGroupForSortID( int sortGroup, int sortID ) const
  153. {
  154. return GetGroupByIndex(GetIndexForSortID(sortGroup,sortID));
  155. }
  156. #if !defined(_PS3)
  157. void EnsureMaxSortIDs( int newMaxSortIDs );
  158. #endif
  159. private:
  160. void InitGroup( surfacesortgroup_t *pGrup );
  161. #ifdef _PS3
  162. bool IsGroupUsed( int groupIndex ) const { return (m_groupIndices[groupIndex] != 0xFFFF); }
  163. inline void MarkGroupUsed( int groupIndex ) { m_groupIndices[groupIndex] = m_groupsShared.Count(); m_groupsShared.AddToTail(); }
  164. inline void MarkGroupNotUsed( int groupIndex ) { m_groupIndices[groupIndex] = 0xFFFF; }
  165. #else
  166. bool IsGroupUsed( int groupIndex ) const { return (m_groupUsed[ (groupIndex>>3) ] & (1<<(groupIndex&7))) != 0; }
  167. inline void MarkGroupUsed( int groupIndex ) { m_groupUsed[groupIndex>>3] |= (1<<(groupIndex&7)); }
  168. inline void MarkGroupNotUsed( int groupIndex ) { m_groupUsed[groupIndex>>3] &= ~(1<<(groupIndex&7)); }
  169. #endif
  170. CUtlVector<materiallist_t> m_list;
  171. // On PS3, the sparse array is smaller so that we fit on SPU
  172. // and the Used flags, just become the indirection to remove the sparseness
  173. #ifdef _PS3
  174. CUtlVector<surfacesortgroup_t> m_groupsShared; // Not sparse
  175. CUtlVector<uint16> m_groupIndices; // Sparse
  176. #else
  177. CUtlVector<surfacesortgroup_t> m_groups; // one per sortID per MAT_SORT_GROUP, sparse
  178. CUtlVector<byte> m_groupUsed;
  179. #endif
  180. // list of indices into m_groups in order per MAT_SORT_GROUP, compact
  181. CUtlVector<surfacesortgroup_t *> m_sortGroupLists[MAX_MAT_SORT_GROUPS];
  182. surfacesortgroup_t m_emptyGroup;
  183. int m_maxSortIDs;
  184. int groupOffset[MAX_MAT_SORT_GROUPS];
  185. };
  186. #define MSL_FOREACH_SURFACE_IN_GROUP_BEGIN( _sortList, _group, _pSurface ) \
  187. { \
  188. for ( short _blockIndex = (_group).listHead; _blockIndex != -1; _blockIndex = (_sortList).GetSurfaceBlock(_blockIndex).nextBlock ) \
  189. { \
  190. const materiallist_t *_pList = &(_sortList).GetSurfaceBlock(_blockIndex); \
  191. for ( int _index = 0; _index < _pList->count; ++_index ) \
  192. { \
  193. SurfaceHandle_t _pSurface = _pList->pSurfaces[_index];
  194. #define MSL_FOREACH_SURFACE_IN_GROUP_END( ) \
  195. } \
  196. } \
  197. }
  198. #define MSL_FOREACH_GROUP_BEGIN( _sortList, _sortGroup, _group ) \
  199. { \
  200. const CUtlVector<surfacesortgroup_t *> &_groupList = (_sortList).GetSortList(_sortGroup); \
  201. int _count = _groupList.Count(); \
  202. for ( int _listIndex = 0; _listIndex < _count; ++_listIndex ) \
  203. { \
  204. const surfacesortgroup_t &_group = *_groupList[_listIndex];
  205. #define MSL_FOREACH_GROUP_END( ) \
  206. } \
  207. }
  208. #define MSL_FOREACH_SURFACE_BEGIN( _sortList, _sortGroup, _pSurface ) \
  209. MSL_FOREACH_GROUP_BEGIN(_sortList, _sortGroup, _group ) \
  210. MSL_FOREACH_SURFACE_IN_GROUP_BEGIN( _sortList, _group, _pSurface )
  211. #define MSL_FOREACH_SURFACE_END( ) \
  212. MSL_FOREACH_SURFACE_IN_GROUP_END() \
  213. MSL_FOREACH_GROUP_END()
  214. //-----------------------------------------------------------------------------
  215. // Converts sort infos to lightmap pages
  216. //-----------------------------------------------------------------------------
  217. int SortInfoToLightmapPage( int sortID );
  218. void BuildMSurfaceVerts( const struct worldbrushdata_t *pBrushData, SurfaceHandle_t surfID, Vector *verts, Vector2D *texCoords, Vector2D lightCoords[][4] );
  219. void BuildMSurfacePrimVerts( worldbrushdata_t *pBrushData, mprimitive_t *prim, CMeshBuilder &builder, SurfaceHandle_t surfID );
  220. void BuildMSurfacePrimIndices( worldbrushdata_t *pBrushData, mprimitive_t *prim, CMeshBuilder &builder );
  221. void BuildBrushModelVertexArray(worldbrushdata_t *pBrushData, SurfaceHandle_t surfID, BrushVertex_t* pVerts );
  222. // Used for debugging - force it to release and restore all material system objects.
  223. void ForceMatSysRestore();
  224. //-----------------------------------------------------------------------------
  225. // Methods associated with getting surface data
  226. //-----------------------------------------------------------------------------
  227. struct SurfaceCtx_t
  228. {
  229. int m_LightmapSize[2];
  230. int m_LightmapPageSize[2];
  231. float m_BumpSTexCoordOffset;
  232. Vector2D m_Offset;
  233. Vector2D m_Scale;
  234. };
  235. // Compute a context necessary for creating vertex data
  236. void SurfSetupSurfaceContext( SurfaceCtx_t& ctx, SurfaceHandle_t surfID );
  237. // Compute texture and lightmap coordinates
  238. void SurfComputeTextureCoordinate( SurfaceHandle_t surfID, Vector const& vec, float * RESTRICT uv );
  239. void SurfComputeLightmapCoordinate( SurfaceCtx_t const& ctx, SurfaceHandle_t surfID,
  240. Vector const& vec, Vector2D& uv );
  241. extern ConVar mat_fastspecular;
  242. void MaterialSystem_RegisterPalettedLightmapSurfaces( int numPages, void *pLightmaps );
  243. #endif // GL_MATSYSIFACE_H