oculus1
annotate libovr/Src/Kernel/OVR_Timer.cpp @ 1:e2f9e4603129
added LibOVR and started a simple vr wrapper.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Sep 2013 16:14:59 +0300 |
parents | |
children | b069a5c27388 |
rev | line source |
---|---|
nuclear@1 | 1 /************************************************************************************ |
nuclear@1 | 2 |
nuclear@1 | 3 Filename : OVR_Timer.cpp |
nuclear@1 | 4 Content : Provides static functions for precise timing |
nuclear@1 | 5 Created : September 19, 2012 |
nuclear@1 | 6 Notes : |
nuclear@1 | 7 |
nuclear@1 | 8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. |
nuclear@1 | 9 |
nuclear@1 | 10 Use of this software is subject to the terms of the Oculus license |
nuclear@1 | 11 agreement provided at the time of installation or download, or which |
nuclear@1 | 12 otherwise accompanies this software in either electronic or hard copy form. |
nuclear@1 | 13 |
nuclear@1 | 14 ************************************************************************************/ |
nuclear@1 | 15 |
nuclear@1 | 16 #include "OVR_Timer.h" |
nuclear@1 | 17 |
nuclear@1 | 18 #if defined (OVR_OS_WIN32) |
nuclear@1 | 19 #include <windows.h> |
nuclear@1 | 20 |
nuclear@1 | 21 #else |
nuclear@1 | 22 #include <sys/time.h> |
nuclear@1 | 23 #endif |
nuclear@1 | 24 |
nuclear@1 | 25 namespace OVR { |
nuclear@1 | 26 |
nuclear@1 | 27 //----------------------------------------------------------------------------------- |
nuclear@1 | 28 // ***** Timer Class |
nuclear@1 | 29 |
nuclear@1 | 30 UInt64 Timer::GetProfileTicks() |
nuclear@1 | 31 { |
nuclear@1 | 32 return (GetRawTicks() * MksPerSecond) / GetRawFrequency(); |
nuclear@1 | 33 } |
nuclear@1 | 34 double Timer::GetProfileSeconds() |
nuclear@1 | 35 { |
nuclear@1 | 36 static UInt64 StartTime = GetProfileTicks(); |
nuclear@1 | 37 return TicksToSeconds(GetProfileTicks()-StartTime); |
nuclear@1 | 38 } |
nuclear@1 | 39 |
nuclear@1 | 40 |
nuclear@1 | 41 //------------------------------------------------------------------------ |
nuclear@1 | 42 // *** Win32 Specific Timer |
nuclear@1 | 43 |
nuclear@1 | 44 #if (defined (OVR_OS_WIN32)) |
nuclear@1 | 45 |
nuclear@1 | 46 CRITICAL_SECTION WinAPI_GetTimeCS; |
nuclear@1 | 47 volatile UInt32 WinAPI_OldTime = 0; |
nuclear@1 | 48 volatile UInt32 WinAPI_WrapCounter = 0; |
nuclear@1 | 49 |
nuclear@1 | 50 |
nuclear@1 | 51 UInt32 Timer::GetTicksMs() |
nuclear@1 | 52 { |
nuclear@1 | 53 return timeGetTime(); |
nuclear@1 | 54 } |
nuclear@1 | 55 |
nuclear@1 | 56 UInt64 Timer::GetTicks() |
nuclear@1 | 57 { |
nuclear@1 | 58 DWORD ticks = timeGetTime(); |
nuclear@1 | 59 UInt64 result; |
nuclear@1 | 60 |
nuclear@1 | 61 // On Win32 QueryPerformanceFrequency is unreliable due to SMP and |
nuclear@1 | 62 // performance levels, so use this logic to detect wrapping and track |
nuclear@1 | 63 // high bits. |
nuclear@1 | 64 ::EnterCriticalSection(&WinAPI_GetTimeCS); |
nuclear@1 | 65 |
nuclear@1 | 66 if (WinAPI_OldTime > ticks) |
nuclear@1 | 67 WinAPI_WrapCounter++; |
nuclear@1 | 68 WinAPI_OldTime = ticks; |
nuclear@1 | 69 |
nuclear@1 | 70 result = (UInt64(WinAPI_WrapCounter) << 32) | ticks; |
nuclear@1 | 71 ::LeaveCriticalSection(&WinAPI_GetTimeCS); |
nuclear@1 | 72 |
nuclear@1 | 73 return result * MksPerMs; |
nuclear@1 | 74 } |
nuclear@1 | 75 |
nuclear@1 | 76 UInt64 Timer::GetRawTicks() |
nuclear@1 | 77 { |
nuclear@1 | 78 LARGE_INTEGER li; |
nuclear@1 | 79 QueryPerformanceCounter(&li); |
nuclear@1 | 80 return li.QuadPart; |
nuclear@1 | 81 } |
nuclear@1 | 82 |
nuclear@1 | 83 UInt64 Timer::GetRawFrequency() |
nuclear@1 | 84 { |
nuclear@1 | 85 static UInt64 perfFreq = 0; |
nuclear@1 | 86 if (perfFreq == 0) |
nuclear@1 | 87 { |
nuclear@1 | 88 LARGE_INTEGER freq; |
nuclear@1 | 89 QueryPerformanceFrequency(&freq); |
nuclear@1 | 90 perfFreq = freq.QuadPart; |
nuclear@1 | 91 } |
nuclear@1 | 92 return perfFreq; |
nuclear@1 | 93 } |
nuclear@1 | 94 |
nuclear@1 | 95 void Timer::initializeTimerSystem() |
nuclear@1 | 96 { |
nuclear@1 | 97 timeBeginPeriod(1); |
nuclear@1 | 98 InitializeCriticalSection(&WinAPI_GetTimeCS); |
nuclear@1 | 99 |
nuclear@1 | 100 } |
nuclear@1 | 101 void Timer::shutdownTimerSystem() |
nuclear@1 | 102 { |
nuclear@1 | 103 DeleteCriticalSection(&WinAPI_GetTimeCS); |
nuclear@1 | 104 timeEndPeriod(1); |
nuclear@1 | 105 } |
nuclear@1 | 106 |
nuclear@1 | 107 #else // !OVR_OS_WIN32 |
nuclear@1 | 108 |
nuclear@1 | 109 |
nuclear@1 | 110 //------------------------------------------------------------------------ |
nuclear@1 | 111 // *** Standard OS Timer |
nuclear@1 | 112 |
nuclear@1 | 113 UInt32 Timer::GetTicksMs() |
nuclear@1 | 114 { |
nuclear@1 | 115 return (UInt32)(GetProfileTicks() / 1000); |
nuclear@1 | 116 } |
nuclear@1 | 117 // The profile ticks implementation is just fine for a normal timer. |
nuclear@1 | 118 UInt64 Timer::GetTicks() |
nuclear@1 | 119 { |
nuclear@1 | 120 return GetProfileTicks(); |
nuclear@1 | 121 } |
nuclear@1 | 122 |
nuclear@1 | 123 void Timer::initializeTimerSystem() |
nuclear@1 | 124 { |
nuclear@1 | 125 } |
nuclear@1 | 126 void Timer::shutdownTimerSystem() |
nuclear@1 | 127 { |
nuclear@1 | 128 } |
nuclear@1 | 129 |
nuclear@1 | 130 UInt64 Timer::GetRawTicks() |
nuclear@1 | 131 { |
nuclear@1 | 132 // TODO: prefer rdtsc when available? |
nuclear@1 | 133 |
nuclear@1 | 134 // Return microseconds. |
nuclear@1 | 135 struct timeval tv; |
nuclear@1 | 136 UInt64 result; |
nuclear@1 | 137 |
nuclear@1 | 138 gettimeofday(&tv, 0); |
nuclear@1 | 139 |
nuclear@1 | 140 result = (UInt64)tv.tv_sec * 1000000; |
nuclear@1 | 141 result += tv.tv_usec; |
nuclear@1 | 142 |
nuclear@1 | 143 return result; |
nuclear@1 | 144 } |
nuclear@1 | 145 |
nuclear@1 | 146 UInt64 Timer::GetRawFrequency() |
nuclear@1 | 147 { |
nuclear@1 | 148 return MksPerSecond; |
nuclear@1 | 149 } |
nuclear@1 | 150 |
nuclear@1 | 151 #endif // !OVR_OS_WIN32 |
nuclear@1 | 152 |
nuclear@1 | 153 |
nuclear@1 | 154 |
nuclear@1 | 155 } // OVR |
nuclear@1 | 156 |