Blender V2.61 - r43446
|
00001 00002 /*************************************************************************************************** 00003 ** 00004 ** Real-Time Hierarchical Profiling for Game Programming Gems 3 00005 ** 00006 ** by Greg Hjelstrom & Byon Garrabrant 00007 ** 00008 ***************************************************************************************************/ 00009 00010 // Credits: The Clock class was inspired by the Timer classes in 00011 // Ogre (www.ogre3d.org). 00012 00013 00014 00015 #ifndef QUICK_PROF_H 00016 #define QUICK_PROF_H 00017 00018 //To disable built-in profiling, please comment out next line 00019 //#define BT_NO_PROFILE 1 00020 #ifndef BT_NO_PROFILE 00021 #include <stdio.h>//@todo remove this, backwards compatibility 00022 #include "btScalar.h" 00023 #include "btAlignedAllocator.h" 00024 #include <new> 00025 00026 00027 00028 00029 00030 #define USE_BT_CLOCK 1 00031 00032 #ifdef USE_BT_CLOCK 00033 00035 class btClock 00036 { 00037 public: 00038 btClock(); 00039 00040 btClock(const btClock& other); 00041 btClock& operator=(const btClock& other); 00042 00043 ~btClock(); 00044 00046 void reset(); 00047 00050 unsigned long int getTimeMilliseconds(); 00051 00054 unsigned long int getTimeMicroseconds(); 00055 private: 00056 struct btClockData* m_data; 00057 }; 00058 00059 #endif //USE_BT_CLOCK 00060 00061 00062 00063 00065 class CProfileNode { 00066 00067 public: 00068 CProfileNode( const char * name, CProfileNode * parent ); 00069 ~CProfileNode( void ); 00070 00071 CProfileNode * Get_Sub_Node( const char * name ); 00072 00073 CProfileNode * Get_Parent( void ) { return Parent; } 00074 CProfileNode * Get_Sibling( void ) { return Sibling; } 00075 CProfileNode * Get_Child( void ) { return Child; } 00076 00077 void CleanupMemory(); 00078 void Reset( void ); 00079 void Call( void ); 00080 bool Return( void ); 00081 00082 const char * Get_Name( void ) { return Name; } 00083 int Get_Total_Calls( void ) { return TotalCalls; } 00084 float Get_Total_Time( void ) { return TotalTime; } 00085 00086 protected: 00087 00088 const char * Name; 00089 int TotalCalls; 00090 float TotalTime; 00091 unsigned long int StartTime; 00092 int RecursionCounter; 00093 00094 CProfileNode * Parent; 00095 CProfileNode * Child; 00096 CProfileNode * Sibling; 00097 }; 00098 00100 class CProfileIterator 00101 { 00102 public: 00103 // Access all the children of the current parent 00104 void First(void); 00105 void Next(void); 00106 bool Is_Done(void); 00107 bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); } 00108 00109 void Enter_Child( int index ); // Make the given child the new parent 00110 void Enter_Largest_Child( void ); // Make the largest child the new parent 00111 void Enter_Parent( void ); // Make the current parent's parent the new parent 00112 00113 // Access the current child 00114 const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); } 00115 int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); } 00116 float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); } 00117 00118 // Access the current parent 00119 const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); } 00120 int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); } 00121 float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); } 00122 00123 protected: 00124 00125 CProfileNode * CurrentParent; 00126 CProfileNode * CurrentChild; 00127 00128 CProfileIterator( CProfileNode * start ); 00129 friend class CProfileManager; 00130 }; 00131 00132 00134 class CProfileManager { 00135 public: 00136 static void Start_Profile( const char * name ); 00137 static void Stop_Profile( void ); 00138 00139 static void CleanupMemory(void) 00140 { 00141 Root.CleanupMemory(); 00142 } 00143 00144 static void Reset( void ); 00145 static void Increment_Frame_Counter( void ); 00146 static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; } 00147 static float Get_Time_Since_Reset( void ); 00148 00149 static CProfileIterator * Get_Iterator( void ) 00150 { 00151 00152 return new CProfileIterator( &Root ); 00153 } 00154 static void Release_Iterator( CProfileIterator * iterator ) { delete ( iterator); } 00155 00156 static void dumpRecursive(CProfileIterator* profileIterator, int spacing); 00157 00158 static void dumpAll(); 00159 00160 private: 00161 static CProfileNode Root; 00162 static CProfileNode * CurrentNode; 00163 static int FrameCounter; 00164 static unsigned long int ResetTime; 00165 }; 00166 00167 00170 class CProfileSample { 00171 public: 00172 CProfileSample( const char * name ) 00173 { 00174 CProfileManager::Start_Profile( name ); 00175 } 00176 00177 ~CProfileSample( void ) 00178 { 00179 CProfileManager::Stop_Profile(); 00180 } 00181 }; 00182 00183 00184 #define BT_PROFILE( name ) CProfileSample __profile( name ) 00185 00186 #else 00187 00188 #define BT_PROFILE( name ) 00189 00190 #endif //#ifndef BT_NO_PROFILE 00191 00192 00193 00194 #endif //QUICK_PROF_H 00195 00196