NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Timer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/DLLImports.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
35 class Timer
36 {
37 public:
38  explicit inline Timer (bool started = false) : m_startTicks((started) ? queryTicks() : -1), m_totalTicks(0) {}
39  inline Timer (const Timer& other) { operator=(other); }
40  inline ~Timer (void) {}
41 
42  inline void start (void) { m_startTicks = queryTicks(); }
43  inline void unstart (void) { m_startTicks = -1; }
44  inline F32 getElapsed (void) { return ticksToSecs(getElapsedTicks()); }
45 
46  inline F32 end (void); // return elapsed, total += elapsed, restart
47  inline F32 getTotal (void) const { return ticksToSecs(m_totalTicks); }
48  inline void clearTotal (void) { m_totalTicks = 0; }
49 
50  inline Timer& operator= (const Timer& other);
51 
52  static void staticInit (void);
53  static inline S64 queryTicks (void);
54  static inline F32 ticksToSecs (S64 ticks);
55 
56 private:
57  inline S64 getElapsedTicks (void); // return time since start, start if unstarted
58 
59 private:
60  static F64 s_ticksToSecsCoef;
61  static S64 s_prevTicks;
62 
63  S64 m_startTicks;
64  S64 m_totalTicks;
65 };
66 
67 //------------------------------------------------------------------------
68 
70 {
71  S64 elapsed = getElapsedTicks();
72  m_startTicks += elapsed;
73  m_totalTicks += elapsed;
74  return ticksToSecs(elapsed);
75 }
76 
77 //------------------------------------------------------------------------
78 
80 {
81  m_startTicks = other.m_startTicks;
82  m_totalTicks = other.m_totalTicks;
83  return *this;
84 }
85 
86 //------------------------------------------------------------------------
87 
89 {
90  LARGE_INTEGER ticks;
91  QueryPerformanceCounter(&ticks);
92  ticks.QuadPart = max(s_prevTicks, ticks.QuadPart);
93  s_prevTicks = ticks.QuadPart; // increasing little endian => thread-safe
94  return ticks.QuadPart;
95 }
96 
97 //------------------------------------------------------------------------
98 
100 {
101  if (s_ticksToSecsCoef == -1.0)
102  staticInit();
103  return (F32)((F64)ticks * s_ticksToSecsCoef);
104 }
105 
106 //------------------------------------------------------------------------
107 
108 S64 Timer::getElapsedTicks(void)
109 {
110  S64 curr = queryTicks();
111  if (m_startTicks == -1)
112  m_startTicks = curr;
113  return curr - m_startTicks;
114 }
115 
116 //------------------------------------------------------------------------
117 }
F32 getTotal(void) const
Definition: Timer.hpp:47
static void staticInit(void)
Definition: Timer.cpp:39
void unstart(void)
Definition: Timer.hpp:43
double F64
Definition: Defs.hpp:90
void start(void)
Definition: Timer.hpp:42
Timer(const Timer &other)
Definition: Timer.hpp:39
static S64 queryTicks(void)
Definition: Timer.hpp:88
float F32
Definition: Defs.hpp:89
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
static F32 ticksToSecs(S64 ticks)
Definition: Timer.hpp:99
F32 end(void)
Definition: Timer.hpp:69
signed __int64 S64
Definition: Defs.hpp:98
Timer(bool started=false)
Definition: Timer.hpp:38
Timer & operator=(const Timer &other)
Definition: Timer.hpp:79
~Timer(void)
Definition: Timer.hpp:40
F32 getElapsed(void)
Definition: Timer.hpp:44
void clearTotal(void)
Definition: Timer.hpp:48