Team Fortress 2 Source Code as on 22/4/2020
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.

228 lines
7.4 KiB

  1. //========= Copyright 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 InitMaterialSystem( void );
  38. void ShutdownMaterialSystem( void );
  39. void InitStartupScreen();
  40. void UpdateMaterialSystemConfig( void );
  41. bool MaterialConfigLightingChanged();
  42. void ClearMaterialConfigLightingChanged();
  43. void OverrideMaterialSystemConfig( MaterialSystem_Config_t &config );
  44. void MaterialSystem_RegisterLightmapSurfaces( void );
  45. IMaterial *GetMaterialAtCrossHair( void );
  46. bool SurfHasBumpedLightmaps( SurfaceHandle_t surfID );
  47. bool SurfNeedsBumpedLightmaps( SurfaceHandle_t surfID );
  48. bool SurfHasLightmap( SurfaceHandle_t surfID );
  49. bool SurfNeedsLightmap( SurfaceHandle_t surfID );
  50. void InitWellKnownRenderTargets( void );
  51. void ShutdownWellKnownRenderTargets( void );
  52. void HandleServerAllowColorCorrection();
  53. void InitMaterialSystemConfig( bool bInEditMode );
  54. #ifndef SWDS
  55. # ifdef NEWMESH
  56. extern CUtlVector<IVertexBuffer *> g_WorldStaticMeshes;
  57. # else
  58. extern CUtlVector<IMesh *> g_WorldStaticMeshes;
  59. # endif
  60. #endif
  61. struct materiallist_t
  62. {
  63. short nextBlock;
  64. short count;
  65. msurface2_t *pSurfaces[15];
  66. };
  67. struct surfacesortgroup_t
  68. {
  69. short listHead;
  70. short listTail;
  71. unsigned short vertexCount;
  72. short groupListIndex;
  73. unsigned short vertexCountNoDetail;
  74. unsigned short indexCountNoDetail;
  75. unsigned short triangleCount;
  76. unsigned short surfaceCount;
  77. };
  78. class CMSurfaceSortList
  79. {
  80. public:
  81. void Init( int maxSortIDs, int minMaterialLists );
  82. void Shutdown();
  83. void Reset();
  84. void AddSurfaceToTail( msurface2_t *pSurface, int nSortGroup, int sortID );
  85. msurface2_t *GetSurfaceAtHead( const surfacesortgroup_t &group ) const;
  86. void GetSurfaceListForGroup( CUtlVector<msurface2_t *> &list, const surfacesortgroup_t &group ) const;
  87. inline int GetIndexForSortID( int nSortGroup, int sortID ) const
  88. {
  89. Assert(sortID<m_maxSortIDs);
  90. return groupOffset[nSortGroup] + sortID;
  91. }
  92. inline const surfacesortgroup_t &GetGroupByIndex( int groupIndex ) const
  93. {
  94. if (!IsGroupUsed(groupIndex))
  95. return m_emptyGroup;
  96. return m_groups[groupIndex];
  97. }
  98. inline const CUtlVector<surfacesortgroup_t *> &GetSortList( int nSortGroup ) const
  99. {
  100. return m_sortGroupLists[nSortGroup];
  101. }
  102. inline const materiallist_t &GetSurfaceBlock(short index) const
  103. {
  104. return m_list[index];
  105. }
  106. inline const surfacesortgroup_t &GetGroupForSortID( int sortGroup, int sortID ) const
  107. {
  108. return GetGroupByIndex(GetIndexForSortID(sortGroup,sortID));
  109. }
  110. void EnsureMaxSortIDs( int newMaxSortIDs );
  111. private:
  112. void InitGroup( surfacesortgroup_t *pGrup );
  113. bool IsGroupUsed( int groupIndex ) const { return (m_groupUsed[ (groupIndex>>3) ] & (1<<(groupIndex&7))) != 0; }
  114. inline void MarkGroupUsed( int groupIndex ) { m_groupUsed[groupIndex>>3] |= (1<<(groupIndex&7)); }
  115. inline void MarkGroupNotUsed( int groupIndex ) { m_groupUsed[groupIndex>>3] &= ~(1<<(groupIndex&7)); }
  116. CUtlVector<materiallist_t> m_list;
  117. CUtlVector<surfacesortgroup_t> m_groups; // one per sortID per MAT_SORT_GROUP, sparse
  118. CUtlVector<byte> m_groupUsed;
  119. // list of indices into m_groups in order per MAT_SORT_GROUP, compact
  120. CUtlVector<surfacesortgroup_t *> m_sortGroupLists[MAX_MAT_SORT_GROUPS];
  121. surfacesortgroup_t m_emptyGroup;
  122. int m_maxSortIDs;
  123. int groupOffset[MAX_MAT_SORT_GROUPS];
  124. };
  125. #define MSL_FOREACH_SURFACE_IN_GROUP_BEGIN( _sortList, _group, _pSurface ) \
  126. { \
  127. for ( short _blockIndex = (_group).listHead; _blockIndex != -1; _blockIndex = (_sortList).GetSurfaceBlock(_blockIndex).nextBlock ) \
  128. { \
  129. const materiallist_t *_pList = &(_sortList).GetSurfaceBlock(_blockIndex); \
  130. for ( int _index = 0; _index < _pList->count; ++_index ) \
  131. { \
  132. SurfaceHandle_t _pSurface = _pList->pSurfaces[_index];
  133. #define MSL_FOREACH_SURFACE_IN_GROUP_END( ) \
  134. } \
  135. } \
  136. }
  137. #define MSL_FOREACH_GROUP_BEGIN( _sortList, _sortGroup, _group ) \
  138. { \
  139. const CUtlVector<surfacesortgroup_t *> &_groupList = (_sortList).GetSortList(_sortGroup); \
  140. int _count = _groupList.Count(); \
  141. for ( int _listIndex = 0; _listIndex < _count; ++_listIndex ) \
  142. { \
  143. const surfacesortgroup_t &_group = *_groupList[_listIndex];
  144. #define MSL_FOREACH_GROUP_END( ) \
  145. } \
  146. }
  147. #define MSL_FOREACH_SURFACE_BEGIN( _sortList, _sortGroup, _pSurface ) \
  148. MSL_FOREACH_GROUP_BEGIN(_sortList, _sortGroup, _group ) \
  149. MSL_FOREACH_SURFACE_IN_GROUP_BEGIN( _sortList, _group, _pSurface )
  150. #define MSL_FOREACH_SURFACE_END( ) \
  151. MSL_FOREACH_SURFACE_IN_GROUP_END() \
  152. MSL_FOREACH_GROUP_END()
  153. //-----------------------------------------------------------------------------
  154. // Converts sort infos to lightmap pages
  155. //-----------------------------------------------------------------------------
  156. int SortInfoToLightmapPage( int sortID );
  157. void BuildMSurfaceVerts( const struct worldbrushdata_t *pBrushData, SurfaceHandle_t surfID, Vector *verts, Vector2D *texCoords, Vector2D lightCoords[][4] );
  158. void BuildMSurfacePrimVerts( worldbrushdata_t *pBrushData, mprimitive_t *prim, CMeshBuilder &builder, SurfaceHandle_t surfID );
  159. void BuildMSurfacePrimIndices( worldbrushdata_t *pBrushData, mprimitive_t *prim, CMeshBuilder &builder );
  160. void BuildBrushModelVertexArray(worldbrushdata_t *pBrushData, SurfaceHandle_t surfID, BrushVertex_t* pVerts );
  161. // Used for debugging - force it to release and restore all material system objects.
  162. void ForceMatSysRestore();
  163. //-----------------------------------------------------------------------------
  164. // Methods associated with getting surface data
  165. //-----------------------------------------------------------------------------
  166. struct SurfaceCtx_t
  167. {
  168. int m_LightmapSize[2];
  169. int m_LightmapPageSize[2];
  170. float m_BumpSTexCoordOffset;
  171. Vector2D m_Offset;
  172. Vector2D m_Scale;
  173. };
  174. // Compute a context necessary for creating vertex data
  175. void SurfSetupSurfaceContext( SurfaceCtx_t& ctx, SurfaceHandle_t surfID );
  176. // Compute texture and lightmap coordinates
  177. void SurfComputeTextureCoordinate( SurfaceCtx_t const& ctx, SurfaceHandle_t surfID,
  178. Vector const& vec, Vector2D& uv );
  179. void SurfComputeLightmapCoordinate( SurfaceCtx_t const& ctx, SurfaceHandle_t surfID,
  180. Vector const& vec, Vector2D& uv );
  181. extern ConVar mat_fastspecular;
  182. void MaterialSystem_RegisterPalettedLightmapSurfaces( int numPages, void *pLightmaps );
  183. #endif // GL_MATSYSIFACE_H