# OgreProfiler.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 /*
    00029
    00030     Although the code is original, many of the ideas for the profiler were borrowed from
    00031 "Real-Time In-Game Profiling" by Steve Rabin which can be found in Game Programming
    00032 Gems 1.
    00033
    00034     This code can easily be adapted to your own non-Ogre project. The only code that is
    00035 Ogre-dependent is in the visualization/logging routines and the use of the Timer class.
    00036
    00037     Enjoy!
    00038
    00039 */
    00040
    00041 #ifndef __Profiler_H__
    00042 #define __Profiler_H__
    00043
    00044 #include "OgrePrerequisites.h"
    00045 #include "OgreSingleton.h"
    00046 #include "OgreString.h"
    00047 #include "OgreOverlay.h"
    00048
    00049 #if OGRE_PROFILING == 1
    00050 #   define OgreProfile( a ) Ogre::Profile _OgreProfileInstance( (a) )
    00051 #   define OgreProfileBegin( a ) Ogre::Profiler::getSingleton().beginProfile( (a) )
    00052 #   define OgreProfileEnd( a ) Ogre::Profiler::getSingleton().endProfile( (a) )
    00053 #   define OgreProfileGroup( a, g ) Ogre::Profile _OgreProfileInstance( (a), (g) )
    00054 #   define OgreProfileBeginGroup( a, g ) Ogre::Profiler::getSingleton().beginProfile( (a), (g) )
    00055 #   define OgreProfileEndGroup( a, g ) Ogre::Profiler::getSingleton().endProfile( (a), (g) )
    00056 #else
    00057 #   define OgreProfile( a )
    00058 #   define OgreProfileBegin( a )
    00059 #   define OgreProfileEnd( a )
    00060 #   define OgreProfileGroup( a, g )
    00061 #   define OgreProfileBeginGroup( a, g )
    00062 #   define OgreProfileEndGroup( a, g )
    00063 #endif
    00064
    00065 namespace Ogre {
    00075     enum ProfileGroupMask
    00076     {
    00078         OGREPROF_USER_DEFAULT = 0x00000001,
    00080         OGREPROF_ALL = 0xFF000000,
    00082         OGREPROF_GENERAL = 0x80000000,
    00084         OGREPROF_CULLING = 0x40000000,
    00086         OGREPROF_RENDERING = 0x20000000
    00087     };
    00088
    00099     class _OgreExport Profile : public ProfilerAlloc {
    00100
    00101         public:
    00102             Profile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
    00103             ~Profile();
    00104
    00105         protected:
    00106
    00108             String mName;
    00110             uint32 mGroupID;
    00111
    00112
    00113     };
    00114
    00126     class _OgreExport Profiler : public Singleton<Profiler>, public ProfilerAlloc {
    00127
    00128         public:
    00129             Profiler();
    00130             ~Profiler();
    00131
    00133             void setTimer(Timer* t);
    00134
    00136             Timer* getTimer();
    00137
    00151             void beginProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
    00152
    00167             void endProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
    00168
    00174             void setEnabled(bool enabled);
    00175
    00177             bool getEnabled() const;
    00178
    00183             void enableProfile(const String& profileName);
    00184
    00189             void disableProfile(const String& profileName);
    00190
    00193             void setProfileGroupMask(uint32 mask) { mProfileMask = mask; }
    00196             uint32 getProfileGroupMask() const { return mProfileMask; }
    00197
    00203             bool watchForMax(const String& profileName);
    00204
    00210             bool watchForMin(const String& profileName);
    00211
    00221             bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
    00222
    00224             void logResults();
    00225
    00227             void reset();
    00228
    00229             enum DisplayMode
    00230             {
    00232                 DISPLAY_PERCENTAGE,
    00234                 DISPLAY_MILLISECONDS
    00235             };
    00236
    00239             void setDisplayMode(DisplayMode d) { mDisplayMode = d; }
    00242             DisplayMode getDisplayMode() const { return mDisplayMode; }
    00243
    00245             void setUpdateDisplayFrequency(uint freq);
    00246
    00248             uint getUpdateDisplayFrequency() const;
    00249
    00251             void setOverlayDimensions(Real width, Real height);
    00252
    00254             void setOverlayPosition(Real left, Real top);
    00255
    00256             Real getOverlayWidth() const;
    00257             Real getOverlayHeight() const;
    00258             Real getOverlayLeft() const;
    00259             Real getOverlayTop() const;
    00260
    00276             static Profiler& getSingleton(void);
    00292             static Profiler* getSingletonPtr(void);
    00293
    00294         protected:
    00295
    00297             void initialize();
    00298
    00300             void displayResults();
    00301
    00303             void processFrameStats();
    00304
    00306             void changeEnableState();
    00307
    00309             OverlayContainer* createContainer();
    00310
    00312             OverlayElement* createTextArea(const String& name, Real width, Real height, Real top, Real left,
    00313                                        uint fontSize, const String& caption, bool show = true);
    00314
    00316             OverlayElement* createPanel(const String& name, Real width, Real height, Real top, Real left,
    00317                                     const String& materialName, bool show = true);
    00318
    00320             struct ProfileInstance {
    00321
    00323                 String      name;
    00324
    00326                 String      parent;
    00327
    00329                 ulong       currTime;
    00330
    00333                 ulong       accum;
    00334
    00336                 uint        hierarchicalLvl;
    00337             };
    00338
    00341             struct ProfileFrame {
    00342
    00344                 String  name;
    00345
    00347                 ulong   frameTime;
    00348
    00350                 uint    calls;
    00351
    00353                 uint    hierarchicalLvl;
    00354
    00355             };
    00356
    00358             struct ProfileHistory {
    00359
    00361                 String  name;
    00362
    00364                 Real    currentTimePercent;
    00366                 Real    currentTimeMillisecs;
    00367
    00369                 Real    maxTimePercent;
    00371                 Real    maxTimeMillisecs;
    00372
    00374                 Real    minTimePercent;
    00376                 Real    minTimeMillisecs;
    00377
    00379                 uint    numCallsThisFrame;
    00380
    00382                 Real    totalTimePercent;
    00384                 Real    totalTimeMillisecs;
    00385
    00388                 ulong   totalCalls;
    00389
    00391                 uint    hierarchicalLvl;
    00392
    00393             };
    00394
    00395
    00396             typedef list<ProfileInstance>::type ProfileStack;
    00397             typedef list<ProfileFrame>::type ProfileFrameList;
    00398             typedef list<ProfileHistory>::type ProfileHistoryList;
    00399             typedef map<String, ProfileHistoryList::iterator>::type ProfileHistoryMap;
    00400             typedef map<String, bool>::type DisabledProfileMap;
    00401
    00402             typedef list<OverlayElement*>::type ProfileBarList;
    00403
    00405             ProfileStack mProfiles;
    00406
    00409             ProfileFrameList mProfileFrame;
    00410
    00412             ProfileHistoryList mProfileHistory;
    00413
    00415             ProfileHistoryMap mProfileHistoryMap;
    00416
    00418             DisabledProfileMap mDisabledProfiles;
    00419
    00421             ProfileBarList mProfileBars;
    00422
    00424             bool mInitialized;
    00425
    00427             uint mMaxDisplayProfiles;
    00428
    00430             Overlay* mOverlay;
    00431
    00433             OverlayContainer* mProfileGui;
    00434
    00436             Real mBarHeight;
    00437
    00439             Real mGuiHeight;
    00440
    00442             Real mGuiWidth;
    00443
    00445             Real mGuiLeft;
    00446
    00448             Real mGuiTop;
    00449
    00451             Real mBarIndent;
    00452
    00454             Real mGuiBorderWidth;
    00455
    00457             Real mBarLineWidth;
    00458
    00460             Real mBarSpacing;
    00461
    00464             uint mUpdateDisplayFrequency;
    00465
    00467             uint mCurrentFrame;
    00468
    00470             Timer* mTimer;
    00471
    00473             ulong mTotalFrameTime;
    00474
    00476             bool mEnabled;
    00477
    00480             bool mEnableStateChangePending;
    00481
    00484             bool mNewEnableState;
    00485
    00487             uint32 mProfileMask;
    00488
    00490             DisplayMode mDisplayMode;
    00491
    00493             ulong mMaxTotalFrameTime;
    00494
    00496             Real mAverageFrameTime;
    00497             bool mResetExtents;
    00498
    00499
    00500     }; // end class
    00504 } // end namespace
    00505
    00506 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52