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.

203 lines
7.5 KiB

  1. //===== Copyright 1996-2007, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef BRUSHBATCHRENDER_H
  9. #define BRUSHBATCHRENDER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. // UNDONE: These are really guesses. Do we ever exceed these limits?
  14. const int MAX_TRANS_NODES = 256;
  15. const int MAX_TRANS_DECALS = 256;
  16. const int MAX_TRANS_BATCHES = 1024;
  17. const int MAX_TRANS_SURFACES = 1024;
  18. class CBrushBatchRender
  19. {
  20. public:
  21. // These are the compact structs produced by the brush render cache. The goal is to have a compact
  22. // list of drawing instructions for drawing an opaque brush model in the most optimal order.
  23. // These structs contain ONLY the opaque surfaces of a brush model.
  24. struct brushrendersurface_t
  25. {
  26. short surfaceIndex;
  27. short planeIndex;
  28. };
  29. // a batch is a list of surfaces with the same material - they can be drawn with one call to the materialsystem
  30. struct brushrenderbatch_t
  31. {
  32. short firstSurface;
  33. short surfaceCount;
  34. IMaterial *pMaterial;
  35. int sortID;
  36. int indexCount;
  37. };
  38. // a mesh is a list of batches with the same vertex format.
  39. struct brushrendermesh_t
  40. {
  41. short firstBatch;
  42. short batchCount;
  43. };
  44. // This is the top-level struct containing all data necessary to render an opaque brush model in optimal order
  45. struct brushrender_t
  46. {
  47. // UNDONE: Compact these arrays into a single allocation
  48. // UNDONE: Compact entire struct to a single allocation? Store brushrender_t * in the linked list?
  49. ~brushrender_t()
  50. {
  51. delete[] pPlanes;
  52. delete[] pMeshes;
  53. delete[] pBatches;
  54. delete[] pSurfaces;
  55. pPlanes = NULL;
  56. pMeshes = NULL;
  57. pBatches = NULL;
  58. pSurfaces = NULL;
  59. }
  60. cplane_t **pPlanes;
  61. brushrendermesh_t *pMeshes;
  62. brushrenderbatch_t *pBatches;
  63. brushrendersurface_t *pSurfaces;
  64. short planeCount;
  65. short meshCount;
  66. short batchCount;
  67. short surfaceCount;
  68. short totalIndexCount;
  69. short totalVertexCount;
  70. };
  71. // Surfaces are stored in a list like this temporarily for sorting purposes only. The compact structs do not store these.
  72. struct surfacelist_t
  73. {
  74. SurfaceHandle_t surfID;
  75. short surfaceIndex;
  76. short planeIndex;
  77. };
  78. // Builds a transrender_t, then executes it's drawing commands
  79. void DrawTranslucentBrushModel( IMatRenderContext *pRenderContext, model_t *model, IClientEntity *baseentity );
  80. void LevelInit();
  81. brushrender_t *FindOrCreateRenderBatch( model_t *pModel );
  82. void DrawOpaqueBrushModel( IMatRenderContext *pRenderContext, IClientEntity *baseentity, model_t *model, ERenderDepthMode_t DepthMode );
  83. void DrawTranslucentBrushModel( IMatRenderContext *pRenderContext, IClientEntity *baseentity, model_t *model, ERenderDepthMode_t DepthMode, bool bDrawOpaque, bool bDrawTranslucent );
  84. void DrawBrushModelShadow( IMatRenderContext *pRenderContext, model_t *model, IClientRenderable *pRenderable );
  85. void DrawBrushModelArray( IMatRenderContext* pRenderContext, int nCount, const BrushArrayInstanceData_t *pInstanceData );
  86. void DrawBrushModelShadowArray( IMatRenderContext* pRenderContext, int nCount, const BrushArrayInstanceData_t *pInstanceData, int nModelTypeFlags );
  87. private:
  88. struct BrushBatchRenderData_t
  89. {
  90. const BrushArrayInstanceData_t *m_pInstanceData;
  91. IMaterial *m_pMaterial;
  92. brushrender_t *m_pBrushRender;
  93. uint16 m_nBatchIndex : 15;
  94. uint16 m_nHasPaintedSurfaces : 1;
  95. int16 m_nLightmapPage : 15;
  96. uint16 m_nIsAlphaTested : 1;
  97. };
  98. // These are the compact structs produced for translucent brush models. These structs contain
  99. // only the translucent surfaces of a brush model.
  100. // a batch is a list of surfaces with the same material - they can be drawn with one call to the materialsystem
  101. struct transbatch_t
  102. {
  103. short firstSurface;
  104. short surfaceCount;
  105. IMaterial *pMaterial;
  106. int sortID;
  107. int indexCount;
  108. };
  109. // This is a list of surfaces that have decals.
  110. struct transdecal_t
  111. {
  112. short firstSurface;
  113. short surfaceCount;
  114. };
  115. // A node is the list of batches that can be drawn without sorting errors. When no decals are present, surfaces
  116. // from the next node may be appended to this one to improve performance without causing sorting errors.
  117. struct transnode_t
  118. {
  119. short firstBatch;
  120. short batchCount;
  121. short firstDecalSurface;
  122. short decalSurfaceCount;
  123. };
  124. // This is the top-level struct containing all data necessary to render a translucent brush model in optimal order.
  125. // NOTE: Unlike the opaque struct, the order of the batches is view-dependent, so caching this is pointless since
  126. // the view usually changes.
  127. struct transrender_t
  128. {
  129. transnode_t nodes[MAX_TRANS_NODES];
  130. SurfaceHandle_t surfaces[MAX_TRANS_SURFACES];
  131. SurfaceHandle_t decalSurfaces[MAX_TRANS_DECALS];
  132. transbatch_t batches[MAX_TRANS_BATCHES];
  133. transbatch_t *pLastBatch; // These are used to append surfaces to existing batches across nodes.
  134. transnode_t *pLastNode; // This improves performance.
  135. short nodeCount;
  136. short batchCount;
  137. short surfaceCount;
  138. short decalSurfaceCount;
  139. };
  140. struct BrushInstanceGroup_t
  141. {
  142. BrushBatchRenderData_t *m_pRenderData;
  143. IMaterial *m_pActualMaterial;
  144. IMaterial *m_pMaterial;
  145. uint16 m_nCount : 15;
  146. uint16 m_nHasPaintedSurfaces : 1;
  147. uint16 m_nIndexCount;
  148. };
  149. private:
  150. // build node lists
  151. void BuildTransLists_r( transrender_t &render, model_t *model, mnode_t *node );
  152. void DrawTransLists( IMatRenderContext *pRenderContext, transrender_t &render, void *pProxyData );
  153. void AddSurfaceToBatch( transrender_t &render, transnode_t *pNode, transbatch_t *pBatch, SurfaceHandle_t surfID );
  154. void AddTransNode( transrender_t &render );
  155. void AddTransBatch( transrender_t &render, SurfaceHandle_t surfID );
  156. void BuildBatchListToDraw( int nCount, const BrushArrayInstanceData_t *pInstanceData,
  157. CUtlVectorFixedGrowable< BrushBatchRenderData_t, 1024 > &batchesToRender, brushrender_t **ppBrushRender );
  158. bool DrawSortedBatchList( IMatRenderContext* pRenderContext, int nCount, BrushInstanceGroup_t *pInstanceGroup, int nMaxInstanceCount );
  159. void DrawPaintForBatches( IMatRenderContext* pRenderContext, int nCount, const BrushInstanceGroup_t *pInstanceGroup, int nMaxInstanceCount );
  160. void ComputeLightmapPages( int nCount, BrushBatchRenderData_t *pRenderData );
  161. void ClearRenderHandles();
  162. int ComputeInstanceGroups( IMatRenderContext *pRenderContext, int nCount, BrushBatchRenderData_t *pRenderData, CUtlVectorFixedGrowable< BrushInstanceGroup_t, 512 > &instanceGroups );
  163. void DrawArrayDebugInformation( IMatRenderContext *pRenderContext, int nCount, const BrushBatchRenderData_t *pRenderData );
  164. void DrawDecalsForBatches( IMatRenderContext *pRenderContext, int nCount, const BrushArrayInstanceData_t *pInstanceData, brushrender_t **ppBrushRender );
  165. void BuildShadowBatchListToDraw( int nCount, const BrushArrayInstanceData_t *pInstanceData,
  166. CUtlVectorFixedGrowable< BrushBatchRenderData_t, 1024 > &batchesToRender, int nModelTypeFlags );
  167. void DrawShadowBatchList( IMatRenderContext* pRenderContext, int nCount, BrushInstanceGroup_t *pInstanceGroup, int nMaxInstanceCount );
  168. static int __cdecl SurfaceCmp(const surfacelist_t *s0, const surfacelist_t *s1 );
  169. static bool __cdecl BatchSortLessFunc( const BrushBatchRenderData_t &left, const BrushBatchRenderData_t &right );
  170. static bool __cdecl ShadowSortLessFunc( const BrushBatchRenderData_t &left, const BrushBatchRenderData_t &right );
  171. CThreadFastMutex m_Mutex;
  172. CUtlLinkedList<brushrender_t*> m_renderList;
  173. };
  174. #if !defined( DEDICATED )
  175. extern CBrushBatchRender g_BrushBatchRenderer;
  176. #endif
  177. #endif // BRUSHBATCHRENDER_H