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.

224 lines
6.0 KiB

  1. //========= Copyright 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. class IMaterialInternal;
  17. //-----------------------------------------------------------------------------
  18. // Special call queue that knows (a) single threaded access, and (b) all
  19. // functions called after last function added
  20. //-----------------------------------------------------------------------------
  21. class CMatCallQueue
  22. {
  23. public:
  24. CMatCallQueue()
  25. {
  26. MEM_ALLOC_CREDIT_( "CMatCallQueue.m_Allocator" );
  27. #ifdef SWDS
  28. m_Allocator.Init( 2*1024, 0, 0, 4 );
  29. #else
  30. m_Allocator.Init( IsX360() ? 2*1024*1024 : 8*1024*1024, 64*1024, 256*1024, 4 );
  31. #endif
  32. m_FunctorFactory.SetAllocator( &m_Allocator );
  33. m_pHead = m_pTail = NULL;
  34. }
  35. size_t GetMemoryUsed()
  36. {
  37. return m_Allocator.GetUsed();
  38. }
  39. int Count()
  40. {
  41. int i = 0;
  42. Elem_t *pCurrent = m_pHead;
  43. while ( pCurrent )
  44. {
  45. i++;
  46. pCurrent = pCurrent->pNext;
  47. }
  48. return i;
  49. }
  50. void CallQueued()
  51. {
  52. if ( !m_pHead )
  53. {
  54. return;
  55. }
  56. CFunctor *pFunctor;
  57. Elem_t *pCurrent = m_pHead;
  58. while ( pCurrent )
  59. {
  60. pFunctor = pCurrent->pFunctor;
  61. #ifdef _DEBUG
  62. if ( pFunctor->m_nUserID == m_nBreakSerialNumber)
  63. {
  64. m_nBreakSerialNumber = (unsigned)-1;
  65. }
  66. #endif
  67. (*pFunctor)();
  68. pFunctor->Release();
  69. pCurrent = pCurrent->pNext;
  70. }
  71. m_Allocator.FreeAll( false );
  72. m_pHead = m_pTail = NULL;
  73. }
  74. void QueueFunctor( CFunctor *pFunctor )
  75. {
  76. Assert( pFunctor );
  77. QueueFunctorInternal( RetAddRef( pFunctor ) );
  78. }
  79. void Flush()
  80. {
  81. if ( !m_pHead )
  82. {
  83. return;
  84. }
  85. CFunctor *pFunctor;
  86. Elem_t *pCurrent = m_pHead;
  87. while ( pCurrent )
  88. {
  89. pFunctor = pCurrent->pFunctor;
  90. pFunctor->Release();
  91. pCurrent = pCurrent->pNext;
  92. }
  93. m_Allocator.FreeAll( false );
  94. m_pHead = m_pTail = NULL;
  95. }
  96. #define DEFINE_MATCALLQUEUE_NONMEMBER_QUEUE_CALL(N) \
  97. template <typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  98. void QueueCall(FUNCTION_RETTYPE (*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) FUNC_ARG_FORMAL_PARAMS_##N ) \
  99. { \
  100. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  101. }
  102. //-------------------------------------
  103. #define DEFINE_MATCALLQUEUE_MEMBER_QUEUE_CALL(N) \
  104. template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  105. void QueueCall(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) FUNC_ARG_FORMAL_PARAMS_##N ) \
  106. { \
  107. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pObject, pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  108. }
  109. //-------------------------------------
  110. #define DEFINE_MATCALLQUEUE_CONST_MEMBER_QUEUE_CALL(N) \
  111. template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  112. void QueueCall(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) const FUNC_ARG_FORMAL_PARAMS_##N ) \
  113. { \
  114. QueueFunctorInternal( m_FunctorFactory.CreateFunctor( pObject, pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ) ); \
  115. }
  116. //-------------------------------------
  117. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_NONMEMBER_QUEUE_CALL );
  118. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_MEMBER_QUEUE_CALL );
  119. FUNC_GENERATE_ALL( DEFINE_MATCALLQUEUE_CONST_MEMBER_QUEUE_CALL );
  120. private:
  121. void QueueFunctorInternal( CFunctor *pFunctor )
  122. {
  123. #ifdef _DEBUG
  124. pFunctor->m_nUserID = m_nCurSerialNumber++;
  125. #endif
  126. MEM_ALLOC_CREDIT_( "CMatCallQueue.m_Allocator" );
  127. Elem_t *pNew = (Elem_t *)m_Allocator.Alloc( sizeof(Elem_t) );
  128. if ( m_pTail )
  129. {
  130. m_pTail->pNext = pNew;
  131. m_pTail = pNew;
  132. }
  133. else
  134. {
  135. m_pHead = m_pTail = pNew;
  136. }
  137. pNew->pNext = NULL;
  138. pNew->pFunctor = pFunctor;
  139. }
  140. struct Elem_t
  141. {
  142. Elem_t *pNext;
  143. CFunctor *pFunctor;
  144. };
  145. Elem_t *m_pHead;
  146. Elem_t *m_pTail;
  147. CMemoryStack m_Allocator;
  148. CCustomizedFunctorFactory<CMemoryStack, CRefCounted1<CFunctor, CRefCountServiceDestruct< CRefST > > > m_FunctorFactory;
  149. unsigned m_nCurSerialNumber;
  150. unsigned m_nBreakSerialNumber;
  151. };
  152. class IMaterialProxy;
  153. //-----------------------------------------------------------------------------
  154. // Additional interfaces used internally to the library
  155. //-----------------------------------------------------------------------------
  156. abstract_class IMaterialSystemInternal : public IMaterialSystem
  157. {
  158. public:
  159. // Returns the current material
  160. virtual IMaterial* GetCurrentMaterial() = 0;
  161. virtual int GetLightmapPage( void ) = 0;
  162. // Gets the maximum lightmap page size...
  163. virtual int GetLightmapWidth( int lightmap ) const = 0;
  164. virtual int GetLightmapHeight( int lightmap ) const = 0;
  165. virtual ITexture *GetLocalCubemap( void ) = 0;
  166. // virtual bool RenderZOnlyWithHeightClipEnabled( void ) = 0;
  167. virtual void ForceDepthFuncEquals( bool bEnable ) = 0;
  168. virtual enum MaterialHeightClipMode_t GetHeightClipMode( void ) = 0;
  169. // FIXME: Remove? Here for debugging shaders in CShaderSystem
  170. virtual void AddMaterialToMaterialList( IMaterialInternal *pMaterial ) = 0;
  171. virtual void RemoveMaterial( IMaterialInternal *pMaterial ) = 0;
  172. virtual void RemoveMaterialSubRect( IMaterialInternal *pMaterial ) = 0;
  173. virtual bool InFlashlightMode() const = 0;
  174. // Can we use editor materials?
  175. virtual bool CanUseEditorMaterials() const = 0;
  176. virtual const char *GetForcedTextureLoadPathID() = 0;
  177. virtual CMatCallQueue *GetRenderCallQueue() = 0;
  178. virtual void UnbindMaterial( IMaterial *pMaterial ) = 0;
  179. virtual uint GetRenderThreadId() const = 0 ;
  180. virtual IMaterialProxy *DetermineProxyReplacements( IMaterial *pMaterial, KeyValues *pFallbackKeyValues ) = 0;
  181. };
  182. #endif // IMATERIALSYSTEMINTERNAL_H