HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  ~RE_TimerQuery() override {}
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  // Delaying read out so that this method is nonblocking
83  myEnd.recordTimestamp(r);
84  myHasTime = true;
85  }
86  /// @}
87 
88  /// Returns true if a begin/end was performed. Does not mean the result is
89  /// necessarily available yet.
90  bool hasResult() const { return myHasTime; }
91 
92  /// Returns true if the query result is available from the GL server.
94  {
95  return (myStart.isResultAvailable(r) &&
96  myEnd.isResultAvailable(r));
97  }
98 
99  /// Fetch elapsed render time between begin/end in nanoseconds.
101  {
102  int64 t = myTime;
103 
104  if(myHasTime)
105  t += (myEnd.getTimeStampNS(r) -
106  myStart.getTimeStampNS(r));
107 
108  myHasTime = false;
109 
110  return t;
111  }
112 
113  /// Reset the queries without deleting them.
114  void reset() { myHasTime = false; myTime = 0; }
115 
116  /// Deletes the queries. init() must be called again.
117  void destroy()
118  {
119  myStart.destroy();
120  myEnd.destroy();
121  myHasTime = false;
122  myTime = 0;
123  }
124 
125 private:
126  RE_TimerQuery myStart;
127  RE_TimerQuery myEnd;
128  int64 myTime;
129  bool myHasTime;
130 };
131 
132 #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.
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
GLdouble GLdouble t
Definition: glew.h:1403
bool hasResult() const
Definition: RE_TimerQuery.h:90
long long int64
Definition: SYS_Types.h:116
bool queryCounter(RE_Render *r)
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:93
~RE_TimerQuery() override
Definition: RE_TimerQuery.h:30