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.

272 lines
7.6 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef IMATERIALSYSTEMINTERNAL_H
  9. #define IMATERIALSYSTEMINTERNAL_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "materialsystem/imaterialsystem.h"
  14. #include "tier1/callqueue.h"
  15. #include "tier1/memstack.h"
  16. #include "tier1/fmtstr.h"
  17. #include "tier0/vprof_telemetry.h"
  18. #include "tier0/perfstats.h"
  19. class IMaterialInternal;
  20. //-----------------------------------------------------------------------------
  21. // Special call queue that knows (a) single threaded access, and (b) all
  22. // functions called after last function added
  23. //-----------------------------------------------------------------------------
  24. class CMatCallQueue
  25. {
  26. public:
  27. CMatCallQueue()
  28. {
  29. static int nStackCount = 0;
  30. CFmtStr stackName( "CMatCallQueue.m_Allocator[%d]", nStackCount++ );
  31. #ifdef DEDICATED
  32. m_Allocator.Init( (const char *)stackName, 2*1024, 0, 0, 4 );
  33. #else
  34. m_Allocator.Init( (const char *)stackName, ( IsGameConsole() || IsPlatformPosix() ) ? 2*1024*1024 : 8*1024*1024, 64*1024, 256*1024, 4 );
  35. #endif
  36. m_FunctorFactory.SetAllocator( &m_Allocator );
  37. m_pHead = m_pTail = NULL;
  38. }
  39. size_t GetMemoryUsed()
  40. {
  41. return m_Allocator.GetUsed();
  42. }
  43. int Count()
  44. {
  45. int i = 0;
  46. Elem_t *pCurrent = m_pHead;
  47. while ( pCurrent )
  48. {
  49. i++;
  50. pCurrent = pCurrent->pNext;
  51. }
  52. return i;
  53. }
  54. template <typename T>
  55. T &Copy( T &item )
  56. {
  57. T *pCopy = (T *)m_Allocator.Alloc( sizeof(T) );
  58. memcpy( pCopy, &item, sizeof(T) );
  59. return *pCopy;
  60. }
  61. template <typename T>
  62. T *CopyArray( T *p, int n )
  63. {
  64. T *pCopy = (T *)m_Allocator.Alloc( sizeof(T) * n );
  65. memcpy( (void *)pCopy, p, sizeof(T) * n );
  66. return pCopy;
  67. }
  68. template <const char *>
  69. const char *Copy( const char *psz )
  70. {
  71. int len = V_strlen( psz );
  72. char *pCopy = (char *)m_Allocator.Alloc( len + 1 );
  73. memcpy( pCopy, psz, len + 1 );
  74. return pCopy;
  75. }
  76. void CallQueued()
  77. {
  78. TM_ZONE_PLOT( TELEMETRY_LEVEL1, "RenderThread", TELEMETRY_ZONE_PLOT_SLOT_2 );
  79. PERF_STATS_BLOCK( "RenderThread", PERF_STATS_SLOT_RENDERTHREAD );
  80. if ( !m_pHead )
  81. {
  82. return;
  83. }
  84. CFunctor *pFunctor;
  85. Elem_t *pCurrent = m_pHead;
  86. while ( pCurrent )
  87. {
  88. pFunctor = pCurrent->GetFunctor();
  89. #ifdef _DEBUG
  90. if ( pFunctor->m_nUserID == m_nBreakSerialNumber)
  91. {
  92. m_nBreakSerialNumber = (unsigned)-1;
  93. }
  94. #endif
  95. if ( pCurrent->pNext )
  96. {
  97. PREFETCH360( pCurrent->pNext, 0 );
  98. PREFETCH360( pCurrent->pNext, 128 );
  99. }
  100. (*pFunctor)();
  101. pFunctor->~CFunctor(); // no need to ref count, we're alone here...
  102. pCurrent = pCurrent->pNext;
  103. }
  104. #ifdef DEBUG_MATCALLQUEUE
  105. static int prevHigh = 0;
  106. if ( m_Allocator.GetUsed() > prevHigh )
  107. {
  108. Msg( "***%d\n", m_Allocator.GetUsed() );
  109. prevHigh = m_Allocator.GetUsed();
  110. }
  111. #endif
  112. m_Allocator.FreeAll( false );
  113. m_pHead = m_pTail = NULL;
  114. }
  115. void QueueFunctor( CFunctor *pFunctor )
  116. {
  117. Assert( pFunctor );
  118. m_Allocator.Alloc( sizeof(Elem_t) );
  119. QueueFunctorInternal( m_FunctorFactory.CreateRefCountingFunctor( pFunctor, &CFunctor::operator() ) );
  120. }
  121. void Flush()
  122. {
  123. if ( !m_pHead )
  124. {
  125. return;
  126. }
  127. CFunctor *pFunctor;
  128. Elem_t *pCurrent = m_pHead;
  129. while ( pCurrent )
  130. {
  131. pFunctor = pCurrent->GetFunctor();
  132. pFunctor->Release();
  133. pCurrent = pCurrent->pNext;
  134. }
  135. m_Allocator.FreeAll( false );
  136. m_pHead = m_pTail = NULL;
  137. }
  138. #define DEFINE_MATCALLQUEUE_NONMEMBER_QUEUE_CALL(N) \
  139. template <typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  140. void QueueCall(FUNCTION_RETTYPE (*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) FUNC_ARG_FORMAL_PARAMS_##N ) \
  141. { \
  142. m_Allocator.Alloc( sizeof(Elem_t) ); \
  143. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  144. }
  145. //-------------------------------------
  146. #define DEFINE_MATCALLQUEUE_MEMBER_QUEUE_CALL(N) \
  147. template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  148. void QueueCall(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) FUNC_ARG_FORMAL_PARAMS_##N ) \
  149. { \
  150. m_Allocator.Alloc( sizeof(Elem_t) ); \
  151. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pObject, pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  152. }
  153. //-------------------------------------
  154. #define DEFINE_MATCALLQUEUE_CONST_MEMBER_QUEUE_CALL(N) \
  155. template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  156. void QueueCall(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) const FUNC_ARG_FORMAL_PARAMS_##N ) \
  157. { \
  158. m_Allocator.Alloc( sizeof(Elem_t) ); \
  159. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pObject, pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  160. }
  161. //-------------------------------------
  162. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_NONMEMBER_QUEUE_CALL );
  163. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_MEMBER_QUEUE_CALL );
  164. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_CONST_MEMBER_QUEUE_CALL );
  165. private:
  166. void QueueFunctorInternal( CFunctor *pFunctor )
  167. {
  168. #ifdef _DEBUG
  169. pFunctor->m_nUserID = m_nCurSerialNumber++;
  170. #endif
  171. MEM_ALLOC_CREDIT_( "CMatCallQueue.m_Allocator" );
  172. // Caller is expected to have preallocated Elem_t entry immediately prior to functor
  173. Elem_t *pNew = ((Elem_t *)pFunctor) - 1;
  174. if ( m_pTail )
  175. {
  176. m_pTail->pNext = pNew;
  177. m_pTail = pNew;
  178. }
  179. else
  180. {
  181. m_pHead = m_pTail = pNew;
  182. }
  183. pNew->pNext = NULL;
  184. }
  185. struct Elem_t
  186. {
  187. Elem_t *pNext;
  188. CFunctor *GetFunctor() { return (CFunctor *)(this + 1 ); }
  189. };
  190. Elem_t *m_pHead;
  191. Elem_t *m_pTail;
  192. CMemoryStack m_Allocator;
  193. CCustomizedFunctorFactory<CMemoryStack, CRefCounted1<CFunctor, CRefCountServiceDestruct< CRefST > > > m_FunctorFactory;
  194. unsigned m_nCurSerialNumber;
  195. unsigned m_nBreakSerialNumber;
  196. };
  197. #define MATCONFIG_FLAGS_SUPPORT_EDITOR ( 1 << 0 )
  198. #define MATCONFIG_FLAGS_SUPPORT_GBUFFER ( 1 << 1 )
  199. //-----------------------------------------------------------------------------
  200. // Additional interfaces used internally to the library
  201. //-----------------------------------------------------------------------------
  202. abstract_class IMaterialSystemInternal : public IMaterialSystem
  203. {
  204. public:
  205. // Returns the current material
  206. virtual IMaterial* GetCurrentMaterial() = 0;
  207. virtual int GetLightmapPage( void ) = 0;
  208. // Gets the maximum lightmap page size...
  209. virtual int GetLightmapWidth( int lightmap ) const = 0;
  210. virtual int GetLightmapHeight( int lightmap ) const = 0;
  211. virtual ITexture *GetLocalCubemap( void ) = 0;
  212. // virtual bool RenderZOnlyWithHeightClipEnabled( void ) = 0;
  213. virtual void ForceDepthFuncEquals( bool bEnable ) = 0;
  214. virtual enum MaterialHeightClipMode_t GetHeightClipMode( void ) = 0;
  215. // FIXME: Remove? Here for debugging shaders in CShaderSystem
  216. virtual void AddMaterialToMaterialList( IMaterialInternal *pMaterial ) = 0;
  217. virtual void RemoveMaterial( IMaterialInternal *pMaterial ) = 0;
  218. virtual void RemoveMaterialSubRect( IMaterialInternal *pMaterial ) = 0;
  219. virtual bool InFlashlightMode() const = 0;
  220. virtual bool IsCascadedShadowMapping() const = 0;
  221. // Can we use editor materials?
  222. virtual bool CanUseEditorMaterials() const = 0;
  223. virtual int GetConfigurationFlags( void ) const = 0;
  224. virtual const char *GetForcedTextureLoadPathID() = 0;
  225. virtual CMatCallQueue *GetRenderCallQueue() = 0;
  226. virtual void UnbindMaterial( IMaterial *pMaterial ) = 0;
  227. virtual uint GetRenderThreadId() const = 0 ;
  228. };
  229. #endif // IMATERIALSYSTEMINTERNAL_H