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.

141 lines
4.4 KiB

  1. //====== Copyright (c), Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Provides a frame function manager that allows for different parts
  4. // of a GC to hook into per frame updated
  5. //
  6. //=============================================================================
  7. #ifndef FRAMEFUNCTION_H
  8. #define FRAMEFUNCTION_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. namespace GCSDK
  13. {
  14. //the generic interface for frame functions. These can be derived from and should execute the work in BRun. It returns a boolean indicating whether or not it has
  15. //more work to complete
  16. class CBaseFrameFunction
  17. {
  18. public:
  19. //the different tiers of functions that can have functions tied to them
  20. enum EFrameType
  21. {
  22. k_EFrameType_RunOnce, // called once per frame
  23. k_EFrameType_RunUntilCompleted, // run tasks that shouldn't wait a frame between execution (gets called at least once per frame, more if work to do & we have time)
  24. k_EFrameType_RunUntilCompletedLowPri, // as above, but only runs if k_EFrameTypeRunUntilCompleted function return they have no remaining work to do
  25. //must come last
  26. k_EFrameType_Count
  27. };
  28. CBaseFrameFunction( const char *pchName, EFrameType eFrameType );
  29. virtual ~CBaseFrameFunction();
  30. // runs the item
  31. virtual bool BRun( const CLimitTimer &limitTimer ) = 0;
  32. //called to handle registering for updates and unregistering. Not registered by default
  33. void Register();
  34. void Deregister();
  35. bool IsRegistered() const { return m_bRegistered; }
  36. protected:
  37. //the frame function manager has access to the internals to avoid exposing them to derived classes
  38. friend class CFrameFunctionMgr;
  39. //let the frame function manager access internals for profiling
  40. CUtlString m_sName; // function name (for debugging)
  41. uint64 m_nTrackedTime; // how much time we have tracked with this frame function
  42. uint32 m_nNumCalls; // the number of ticks that this has been associated with
  43. EFrameType m_EFrameType; // what kind of frame function is this
  44. bool m_bRegistered; // are we currently registered?
  45. };
  46. //a utility class that helps handle registering for frame functions and adapts it to a member function call
  47. template < class T >
  48. class CFrameFunction :
  49. public CBaseFrameFunction
  50. {
  51. public:
  52. typedef bool ( T::*func_t )( const CLimitTimer &limitTimer );
  53. //construct and register all in one
  54. CFrameFunction( T *pObj, func_t func, const char *pchName, EFrameType eFrameType ) :
  55. CBaseFrameFunction( pchName, eFrameType ), m_pObj( NULL ), m_Func( NULL )
  56. {
  57. Register( pObj, func );
  58. }
  59. //construct, but don't immediately register for updates
  60. CFrameFunction( const char *pchName, EFrameType eFrameType ) :
  61. CBaseFrameFunction( pchName, eFrameType ), m_pObj( NULL ), m_Func( NULL )
  62. {
  63. }
  64. void Register( T *pObj, func_t func )
  65. {
  66. m_pObj = pObj;
  67. m_Func = func;
  68. CBaseFrameFunction::Register();
  69. }
  70. virtual bool BRun( const CLimitTimer &limitTimer )
  71. {
  72. return (m_pObj->*m_Func)( limitTimer );
  73. }
  74. virtual bool BAllocedSeparately() { return false; }
  75. private:
  76. T *m_pObj;
  77. func_t m_Func;
  78. };
  79. //the main manager that handles registration of all the frame functions and tracks performance and dispatches
  80. //appropriately
  81. class CFrameFunctionMgr
  82. {
  83. public:
  84. CFrameFunctionMgr();
  85. //called to register a main loop function for the specified tier
  86. void Register( CBaseFrameFunction* pFrameFunc );
  87. //handles unregistering a main loop function
  88. void Deregister( CBaseFrameFunction* pFrameFunc );
  89. //called to execute the main frame. This should preceded individual frame ticks
  90. void RunFrame( const CLimitTimer& limitTimer );
  91. //called within a frame for each tick
  92. bool RunFrameTick( const CLimitTimer& limitTimer );
  93. //called to dump a listing of the performance timings of all the frame functions that are registered
  94. void DumpProfile();
  95. //resets the profile stats
  96. void ClearProfile();
  97. private:
  98. //sort function
  99. static bool SortFrameFuncByTime( const CBaseFrameFunction* pLhs, const CBaseFrameFunction* pRhs );
  100. //called to update the frame functions associated with the specified list
  101. bool RunFrameList( CBaseFrameFunction::EFrameType eType, const CLimitTimer& limitTimer );
  102. //the listing of subscribed
  103. CUtlVector< CBaseFrameFunction* > m_MainLoopFrameFuncs[ CBaseFrameFunction::k_EFrameType_Count ];
  104. //the number of frames that we have profiled
  105. uint32 m_nNumProfileFrames;
  106. //have we completed all of the high priority work for this frame?
  107. bool m_bCompletedHighPri;
  108. };
  109. //the global frame function manager
  110. CFrameFunctionMgr& GFrameFunctionMgr();
  111. } //namespace GCSDK
  112. #endif