HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RE_TimerQuery.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_TimerQuery.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  *
10  * This class implements the GL_ARB_timer_query feature, which allows
11  * timer start/stop commands to be inserted into the GL command stream so
12  * that the time it takes the hardware to complete a task is accurately
13  * measured. Simply timing the GL commands themselves is not sufficient
14  * because OpenGL processes them asynchronously.
15  *
16  * The body of this class is implemented in RE_OGLQuery.C.
17  */
18 #ifndef RE_TIMER_QUERY_H
19 #define RE_TIMER_QUERY_H
20 
21 #include "RE_OGLQuery.h"
22 
23 class RE_Render;
24 
25 /// Basic GL timer query implementation
27 {
28 public:
29  RE_TimerQuery();
30  virtual ~RE_TimerQuery() {}
31 
32  /// @brief Fetch elapsed render time between begin/end in nanoseconds.
33  /// Returns the elapsed time in nanoseconds. If this extension is not
34  /// supported, it will always return 0 and assert. Calling this
35  /// without previously calling begin/end, or inside begin/end, will also
36  /// assert.
38  { return getLongResult(r); }
39 
40  /// Alternate query - request the GPU timestamp. This is useful because timer
41  /// queries cannot be nested, so higher level intervals can be computed
42  /// by differencing 2 timestamps. RE_GPUTimer does this.
43  bool recordTimestamp(RE_Render *r) { return queryCounter(r); }
44 
45  /// Fetch the timestamp recorded by recordTimestamp()
47  { return getLongResult(r); }
48 };
49 
50 /// Regular begin/end Timer queries cannot be nested, so this class uses 2
51 /// timers to record the start and end timestamp instead.
53 {
54 public:
55  RE_GPUTimer() : myHasTime(false), myTime(0) {}
56 
57  /// Initialize the queries (queries cannot be created if a query is running)
58  void init(RE_Render *r)
59  {
60  myStart.init(r);
61  myEnd.init(r);
62  myTime = 0;
63  myHasTime = false;
64  }
65 
66  /// @brief Mark the beginning and end of GPU commands to time
67  /// Bracket the draw calls to measure with begin and end. Multiple begin/end
68  /// calls without a reset() will accumulate time.
69  /// @{
70  void begin(RE_Render *r)
71  {
72  if(myHasTime)
73  {
74  myTime += getElapsedTimeNS(r);
75  myHasTime = false;
76  }
77  myStart.recordTimestamp(r);
78  }
79 
80  void end(RE_Render *r)
81  {
82  myEnd.recordTimestamp(r);
83  myHasTime = true;
84  }
85  /// @}
86 
87  /// Returns true if a begin/end was performed. Does not mean the result is
88  /// necessarily available yet.
89  bool hasResult() const { return myHasTime; }
90 
91  /// Returns true if the query result is available from the GL server.
93  {
94  return (myStart.isResultAvailable(r) &&
95  myEnd.isResultAvailable(r));
96  }
97 
98  /// Fetch elapsed render time between begin/end in nanoseconds.
100  {
101  int64 t = myTime;
102 
103  if(myHasTime)
104  t += (myEnd.getTimeStampNS(r) -
105  myStart.getTimeStampNS(r));
106 
107  myHasTime = false;
108 
109  return t;
110  }
111 
112  /// Reset the queries without deleting them.
113  void reset() { myHasTime = false; myTime = 0; }
114 
115  /// Deletes the queries. init() must be called again.
116  void destroy()
117  {
118  myStart.destroy();
119  myEnd.destroy();
120  myHasTime = false;
121  myTime = 0;
122  }
123 
124 private:
125  RE_TimerQuery myStart;
126  RE_TimerQuery myEnd;
127  int64 myTime;
128  bool myHasTime;
129 };
130 
131 #endif
bool recordTimestamp(RE_Render *r)
Definition: RE_TimerQuery.h:43
void end(RE_Render *r)
Mark the beginning and end of GPU commands to time Bracket the draw calls to measure with begin and e...
Definition: RE_TimerQuery.h:80
int64 getElapsedTimeNS(RE_Render *r)
Fetch elapsed render time between begin/end in nanoseconds.
Definition: RE_TimerQuery.h:99
int64 getLongResult(RE_Render *r)
#define RE_API
Definition: RE_API.h:10
void reset()
Reset the queries without deleting them.
void begin(RE_Render *r)
Mark the beginning and end of GPU commands to time Bracket the draw calls to measure with begin and e...
Definition: RE_TimerQuery.h:70
bool hasResult() const
Definition: RE_TimerQuery.h:89
long long int64
Definition: SYS_Types.h:100
bool queryCounter(RE_Render *r)
virtual ~RE_TimerQuery()
Definition: RE_TimerQuery.h:30
Basic GL timer query implementation.
Definition: RE_TimerQuery.h:26
void destroy()
Deletes the queries. init() must be called again.
int64 getTimeStampNS(RE_Render *r)
Fetch the timestamp recorded by recordTimestamp()
Definition: RE_TimerQuery.h:46
void init(RE_Render *r)
Initialize the queries (queries cannot be created if a query is running)
Definition: RE_TimerQuery.h:58
int64 getElapsedTimeNS(RE_Render *r)
Fetch elapsed render time between begin/end in nanoseconds. Returns the elapsed time in nanoseconds...
Definition: RE_TimerQuery.h:37
GLboolean r
Definition: glcorearb.h:1221
bool isResultAvailable(RE_Render *r)
Returns true if the query result is available from the GL server.
Definition: RE_TimerQuery.h:92