00001 #pragma once
00002
00003 #ifndef PROFILER_H
00004 #define PROFILER_H
00005
00006 #ifdef RT_PROFILER
00007
00008 #define MAX_GROUP_PROFILES 5
00009 #define MAX_PASS_PROFILES 120
00010 #define MAX_MODEL_PROFILES 10
00011
00012 #include <string>
00013 #include <vector>
00014 #include <iostream>
00015
00016 #include "Types.h"
00017 #include "Stopwatch.h"
00018
00019 namespace RenderTools {
00020
00021 using namespace std;
00022
00023 #ifdef RT_NVPERFKIT
00024
00025 #define COUNTER_DISABLED 0xFFFFFFFF
00026 #define COUNTER_ENTRY_COUNT 10
00027 #define BUFFER_ENTRY_COUNT 10
00028 #define MAX_COUNTERS 100
00029
00030 class NVDataProvider {
00031 public:
00032
00033 NVDataProvider();
00034
00035 void initialise();
00036 unsigned int add( unsigned int counterIndex );
00037 unsigned int add( string name );
00038 bool sample();
00039 string name( unsigned int counterIndex );
00040 float value( unsigned int counterIndex );
00041
00042 static int s_numPasses;
00043
00044 protected:
00045 unsigned int m_counterIndexArray[ COUNTER_ENTRY_COUNT ];
00046 unsigned int m_counterIndexArrayCount;
00047 float m_counterValues[ COUNTER_ENTRY_COUNT ][ BUFFER_ENTRY_COUNT ];
00048 unsigned int m_counterValuesRRIndex;
00049 };
00050
00051 #endif
00052
00053 class Profiler{
00054 public:
00055
00056 Profiler( string name, string file, int line );
00057 virtual ~Profiler();
00058
00059 void begin();
00060 void end();
00061 double average();
00062 double time();
00063 int calls();
00064 string name();
00065 string file();
00066 int line();
00067
00068 public:
00069 static bool s_init;
00070 static int s_objectID;
00071 static int s_maxObjects;
00072 static int s_numPasses;
00073 static bool s_doExperiment;
00074 static vector< Profiler * > s_profilers;
00075 static vector< pair< UINT, string > > s_counters;
00076 #ifdef RT_NVPERFKIT
00077 static NVDataProvider * s_dataProvider;
00078 #endif
00079 static void shutDown();
00080 static void dumpAll();
00081
00082 #ifdef RT_NVPERFKIT
00083 static float _getValue( UINT64 value, UINT64 cycles, string name );
00084 static void _initialise( bool doExperiment, int numOfObjects );
00085 static void _sample();
00086 static void _dumpSample( bool verbose = true );
00087 static void _dumpResult( NVPMRESULT result );
00088 #endif
00089
00090 private:
00091 int m_calls;
00092 Stopwatch m_stopWatch;
00093 double m_time;
00094 string m_name;
00095 string m_file;
00096 int m_line;
00097
00098 };
00099
00100 #ifdef RT_NVPERFKIT
00101 #define PROFILER_BEGIN_EXPERIMENT( doExperiment, numOfObjects )\
00102 Profiler::_initialise( doExperiment, numOfObjects );\
00103 Profiler::s_numPasses = 1; if( doExperiment ){ NVPMBeginExperiment( &( Profiler::s_numPasses ) ); }else{ Profiler::_sample(); }\
00104 for( int passCounter = 0; passCounter < Profiler::s_numPasses; passCounter++ ){\
00105 if( doExperiment ){ NVPMBeginPass( passCounter ); Profiler::s_objectID = 0; }\
00106
00107 #define PROFILER_END_EXPERIMENT( verbose )\
00108 if( Profiler::s_doExperiment )NVPMEndPass( passCounter ); \
00109 }\
00110 if( Profiler::s_doExperiment ){ NVPMEndExperiment(); }\
00111 Profiler::_sample(); Profiler::_dumpSample( verbose );
00112
00113 #define PROFILER_BEGIN_OBJECT() if( Profiler::s_doExperiment ){ NVPMBeginObject( Profiler::s_objectID++ ); }
00114 #define PROFILER_END_OBJECT() if( Profiler::s_doExperiment ){ NVPMEndObject( Profiler::s_objectID - 1 ); }
00115 #else
00116 #define PROFILER_BEGIN_EXPERIMENT( doExperiment, numOfObjects )
00117 #define PROFILER_END_EXPERIMENT( verbose )
00118
00119 #define PROFILER_BEGIN_OBJECT()
00120 #define PROFILER_END_OBJECT()
00121 #endif // RT_NVPERFKIT
00122
00123 };
00124
00125 #else // RT_PROFILER
00126
00127 #define PROFILER_BEGIN_EXPERIMENT( doExperiment, numOfObjects )
00128 #define PROFILER_END_EXPERIMENT( verbose )
00129 #define PROFILER_BEGIN_OBJECT()
00130 #define PROFILER_END_OBJECT()
00131
00132 #include <string>
00133 using namespace std;
00134
00135 namespace RenderTools {
00136
00137
00138 class Profiler{
00139 public:
00140
00141 Profiler( string name, string file, int line ){}
00142 void begin( void ){}
00143 void end( void ){}
00144 double average( void ){ return( 0.0 ); }
00145 string name( void ){ return( "" ); }
00146 string file( void ){ return( "" ); }
00147 int line( void ){ return( 0 ); }
00148
00149 static void dumpAll( void ){}
00150 static void shutDown( void ){}
00151
00152 #ifdef RT_NVPERFKIT
00153 static float _getValue( UINT64 value, UINT64 cycles, string name ){}
00154 static void _initialise( bool doExperiment, int numOfObjects ){}
00155 static void _sample(){}
00156 static void _dumpSample( bool verbose = true ){}
00157 #endif
00158 };
00159
00160 };
00161
00162 #endif // RT_PROFILER
00163
00164 #endif // PROFILER_H