oculus1

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_Timer.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +/************************************************************************************
     1.5 
     1.6 Filename    :   OVR_Timer.cpp
     1.7 Content     :   Provides static functions for precise timing
     1.8 Created     :   September 19, 2012
     1.9 Notes       : 
    1.10 
    1.11 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.12 
    1.13 Use of this software is subject to the terms of the Oculus license
    1.14 agreement provided at the time of installation or download, or which
    1.15 otherwise accompanies this software in either electronic or hard copy form.
    1.16 
    1.17 ************************************************************************************/
    1.18 
    1.19 #include "OVR_Timer.h"
    1.20 
    1.21 #if defined (OVR_OS_WIN32)
    1.22 #include <windows.h>
    1.23 
    1.24 #else
    1.25 #include <sys/time.h>
    1.26 #endif
    1.27 
    1.28 namespace OVR {
    1.29 
    1.30 //-----------------------------------------------------------------------------------
    1.31 // ***** Timer Class
    1.32 
    1.33 UInt64 Timer::GetProfileTicks()
    1.34 {
    1.35     return (GetRawTicks() * MksPerSecond) / GetRawFrequency();
    1.36 }
    1.37 double Timer::GetProfileSeconds()
    1.38 {
    1.39     static UInt64 StartTime = GetProfileTicks();
    1.40     return TicksToSeconds(GetProfileTicks()-StartTime);
    1.41 }
    1.42 
    1.43 
    1.44 //------------------------------------------------------------------------
    1.45 // *** Win32 Specific Timer
    1.46 
    1.47 #if (defined (OVR_OS_WIN32))
    1.48 
    1.49 CRITICAL_SECTION WinAPI_GetTimeCS;
    1.50 volatile UInt32  WinAPI_OldTime = 0;
    1.51 volatile UInt32  WinAPI_WrapCounter = 0;
    1.52 
    1.53 
    1.54 UInt32 Timer::GetTicksMs()
    1.55 {
    1.56     return timeGetTime();
    1.57 }
    1.58 
    1.59 UInt64 Timer::GetTicks()
    1.60 {
    1.61     DWORD  ticks = timeGetTime();
    1.62     UInt64 result;
    1.63 
    1.64     // On Win32 QueryPerformanceFrequency is unreliable due to SMP and
    1.65     // performance levels, so use this logic to detect wrapping and track
    1.66     // high bits.
    1.67     ::EnterCriticalSection(&WinAPI_GetTimeCS);
    1.68 
    1.69     if (WinAPI_OldTime > ticks)
    1.70         WinAPI_WrapCounter++;
    1.71     WinAPI_OldTime = ticks;
    1.72 
    1.73     result = (UInt64(WinAPI_WrapCounter) << 32) | ticks;
    1.74     ::LeaveCriticalSection(&WinAPI_GetTimeCS);
    1.75 
    1.76     return result * MksPerMs;
    1.77 }
    1.78 
    1.79 UInt64 Timer::GetRawTicks()
    1.80 {
    1.81     LARGE_INTEGER li;
    1.82     QueryPerformanceCounter(&li);
    1.83     return li.QuadPart;
    1.84 }
    1.85 
    1.86 UInt64 Timer::GetRawFrequency()
    1.87 {
    1.88     static UInt64 perfFreq = 0;
    1.89     if (perfFreq == 0)
    1.90     {
    1.91         LARGE_INTEGER freq;
    1.92         QueryPerformanceFrequency(&freq);
    1.93         perfFreq = freq.QuadPart;
    1.94     }
    1.95     return perfFreq;
    1.96 }
    1.97 
    1.98 void Timer::initializeTimerSystem()
    1.99 {
   1.100     timeBeginPeriod(1);
   1.101     InitializeCriticalSection(&WinAPI_GetTimeCS);
   1.102 
   1.103 }
   1.104 void Timer::shutdownTimerSystem()
   1.105 {
   1.106     DeleteCriticalSection(&WinAPI_GetTimeCS);
   1.107     timeEndPeriod(1);
   1.108 }
   1.109 
   1.110 #else   // !OVR_OS_WIN32
   1.111 
   1.112 
   1.113 //------------------------------------------------------------------------
   1.114 // *** Standard OS Timer     
   1.115 
   1.116 UInt32 Timer::GetTicksMs()
   1.117 {
   1.118     return (UInt32)(GetProfileTicks() / 1000);
   1.119 }
   1.120 // The profile ticks implementation is just fine for a normal timer.
   1.121 UInt64 Timer::GetTicks()
   1.122 {
   1.123     return GetProfileTicks();
   1.124 }
   1.125 
   1.126 void Timer::initializeTimerSystem()
   1.127 {
   1.128 }
   1.129 void Timer::shutdownTimerSystem()
   1.130 {
   1.131 }
   1.132 
   1.133 UInt64  Timer::GetRawTicks()
   1.134 {
   1.135     // TODO: prefer rdtsc when available?
   1.136 
   1.137     // Return microseconds.
   1.138     struct timeval tv;
   1.139     UInt64 result;
   1.140 
   1.141     gettimeofday(&tv, 0);
   1.142 
   1.143     result = (UInt64)tv.tv_sec * 1000000;
   1.144     result += tv.tv_usec;
   1.145 
   1.146     return result;
   1.147 }
   1.148 
   1.149 UInt64 Timer::GetRawFrequency()
   1.150 {
   1.151     return MksPerSecond;
   1.152 }
   1.153 
   1.154 #endif  // !OVR_OS_WIN32
   1.155 
   1.156 
   1.157 
   1.158 } // OVR
   1.159 
   1.160 \ No newline at end of file