Browse Source

Timers now use std::chrono
Fixed line endings in the new OpenAudio files

BearishSun 9 years ago
parent
commit
c168935089

+ 13 - 7
Build/VS2015/BansheeUtility.vcxproj

@@ -134,7 +134,8 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ImportLibrary>..\..\lib\x86\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <SubSystem>NotSet</SubSystem>
       <NoEntryPoint>false</NoEntryPoint>
       <EntryPointSymbol>
@@ -157,7 +158,8 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ImportLibrary>..\..\lib\$(Platform)\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <SubSystem>NotSet</SubSystem>
       <NoEntryPoint>false</NoEntryPoint>
       <EntryPointSymbol>
@@ -186,7 +188,8 @@
       <OptimizeReferences>true</OptimizeReferences>
       <ImportLibrary>..\..\lib\x86\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <NoEntryPoint>false</NoEntryPoint>
     </Link>
   </ItemDefinitionGroup>
@@ -212,7 +215,8 @@
       <OptimizeReferences>true</OptimizeReferences>
       <ImportLibrary>..\..\lib\x86\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <NoEntryPoint>false</NoEntryPoint>
     </Link>
   </ItemDefinitionGroup>
@@ -240,7 +244,8 @@
       <OptimizeReferences>true</OptimizeReferences>
       <ImportLibrary>..\..\lib\$(Platform)\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <NoEntryPoint>false</NoEntryPoint>
     </Link>
   </ItemDefinitionGroup>
@@ -266,7 +271,8 @@
       <OptimizeReferences>true</OptimizeReferences>
       <ImportLibrary>..\..\lib\$(Platform)\$(Configuration)\$(TargetName).lib</ImportLibrary>
       <AdditionalDependencies>DbgHelp.lib;IPHLPAPI.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalLibraryDirectories></AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>
+      </AdditionalLibraryDirectories>
       <NoEntryPoint>false</NoEntryPoint>
     </Link>
   </ItemDefinitionGroup>
@@ -294,6 +300,7 @@
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsMemorySerializer.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsPath.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsRect2.cpp" />
+    <ClCompile Include="..\..\Source\BansheeUtility\Source\BsTimer.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsTorus.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsUtil.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\BsVector2I.cpp" />
@@ -309,7 +316,6 @@
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32CrashHandler.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32FileSystem.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32PlatformUtility.cpp" />
-    <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32Timer.cpp" />
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32Window.cpp" />
     <ClInclude Include="..\..\Source\BansheeUtility\Include\BsAny.h" />
     <ClInclude Include="..\..\Source\BansheeUtility\Include\BsBinaryCloner.h" />

+ 3 - 3
Build/VS2015/BansheeUtility.vcxproj.filters

@@ -475,9 +475,6 @@
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32CrashHandler.cpp">
       <Filter>Source Files\Win32</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32Timer.cpp">
-      <Filter>Source Files\Win32</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32PlatformUtility.cpp">
       <Filter>Source Files\Win32</Filter>
     </ClCompile>
@@ -553,5 +550,8 @@
     <ClCompile Include="..\..\Source\BansheeUtility\Source\Win32\BsWin32Window.cpp">
       <Filter>Source Files\Win32</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\Source\BansheeUtility\Source\BsTimer.cpp">
+      <Filter>Source Files\General</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 0 - 2
Documentation/Manuals/Native/porting.md

@@ -33,10 +33,8 @@ Dynamic library loading							| No			| BansheeUtility				| No					| BsDynLib.h/B
 OpenGL initialization*							| No			| BansheeGLRenderAPI			| No					| BsGLUtil.h, BsGLSupport.h/BsWin32GLSupport.cpp, BsWin32Context.h/BsWin32Context.cpp, BsWin32VideoModeInfo.cpp | Initializing the OpenGL context (or other context if non-OpenGL API is used for the port)
 Window creation*								| No			| BansheeUtility, BansheeGLRenderAPI | No				| BsWin32Window.h/BsWin32Window.cpp, BsWin32Platform.h/BsWin32Platform.cpp, BsWin32RenderWindow.h/BsWin32RenderWindow.cpp | Creating and interacting with the window
 OS message loop*								| No			| BansheeCore					| No					| BsWin32Platform.h/BsWin32Platform.cpp 				 | Running the main message loop, responding to its events
-Timer											| No			| BansheeUtility				| No					| BsTimer.h/BsWin32Timer.cpp 							 | Measuring time
 Input*											| No			| BansheeCore					| Maybe					| BsPlatform.h/BsWin32Platform.cpp 						 | Receive input from OS (mouse, keyboard)
 UUID generation									| No			| BansheeUtility				| No					| BsPlatformUtility.h/BsWin32PlatformUtility.cpp 		 | Generate UUID/GUID
-Performance timers								| No			| BansheeUtility				| No					| BsPlatformUtility.h/BsWin32PlatformUtility.cpp 		 | Measure time precisely for profiling
 
 ## Non-critical features {#porting_b_b} 
 A list of non-critical features, and editor-only features, in the rough order they should be implemented. You should be able to get the engine running without these, or with just dummy implementations (that do nothing).

+ 2 - 1
Source/BansheeCore/Include/BsProfilerCPU.h

@@ -39,9 +39,10 @@ namespace BansheeEngine
 			double time;
 		private:
 			double startTime;
+			std::chrono::high_resolution_clock mHRClock;
 
 			/**	Returns time elapsed since CPU was started in millseconds. */
-			static inline double getCurrentTime();
+			inline double getCurrentTime() const;
 		};
 
 		/**	Timer class responsible for tracking number of elapsed CPU cycles. */

+ 9 - 2
Source/BansheeCore/Source/BsProfilerCPU.cpp

@@ -3,6 +3,7 @@
 #include "BsProfilerCPU.h"
 #include "BsDebug.h"
 #include "BsPlatform.h"
+#include <chrono>
 
 #if BS_COMPILER == BS_COMPILER_GNUC || BS_COMPILER == BS_COMPILER_CLANG
 	#include "cpuid.h"
@@ -12,9 +13,12 @@
 	#include "intrin.h"
 #endif
 
+using namespace std::chrono;
+
 namespace BansheeEngine
 {
 	ProfilerCPU::Timer::Timer()
+		:startTime(0.0f)
 	{
 		time = 0.0f;
 	}
@@ -34,9 +38,12 @@ namespace BansheeEngine
 		time = 0.0f;
 	}
 
-	inline double ProfilerCPU::Timer::getCurrentTime() 
+	inline double ProfilerCPU::Timer::getCurrentTime() const
 	{
-		return PlatformUtility::queryPerformanceTimerMs();
+		steady_clock::time_point timeNow = mHRClock.now();
+		nanoseconds timeNowNs = timeNow.time_since_epoch();
+
+		return timeNowNs.count() * 0.000001;
 	}
 
 	ProfilerCPU::TimerPrecise::TimerPrecise()

+ 1 - 1
Source/BansheeEngine/Source/BsSplashScreen.cpp

@@ -65,7 +65,7 @@ namespace BansheeEngine
 		if (m->window == nullptr)
 			return;
 
-		UINT32 currentTime = m->timer.getMilliseconds();
+		UINT64 currentTime = m->timer.getMilliseconds();
 		if (currentTime < SPLASH_SCREEN_DURATION_MS)
 			BS_THREAD_SLEEP(SPLASH_SCREEN_DURATION_MS - currentTime);
 

+ 0 - 8
Source/BansheeUtility/Include/BsPlatformUtility.h

@@ -31,14 +31,6 @@ namespace BansheeEngine
 		 */
 		static void terminate(bool force = false);
 
-		/**
-		 * Queries the internal system performance counter you can use for very precise time measurements. Value is in 
-		 * milliseconds.
-		 *
-		 * @note	Thread safe.
-		 */
-		static double queryPerformanceTimerMs();
-
 		/**
 		 * Adds a string to the clipboard.
 		 *

+ 1 - 1
Source/BansheeUtility/Include/BsTime.h

@@ -88,7 +88,7 @@ namespace BansheeEngine
 		UINT64 mTimeSinceStartMs;
 
 		UINT64 mAppStartTime; /**< Time the application started, in microseconds */
-		unsigned long mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
+		UINT64 mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
 		std::atomic<unsigned long> mCurrentFrame;
 
 		Timer* mTimer;

+ 5 - 12
Source/BansheeUtility/Include/BsTimer.h

@@ -20,33 +20,26 @@ namespace BansheeEngine
     public:
 		/** Construct the timer and start timing. */
 		Timer();
-		~Timer();
 
 		/** Reset the timer to zero. */
 		void reset();
 
 		/** Returns time in milliseconds since timer was initialized or last reset. */
-		unsigned long getMilliseconds();
+		UINT64 getMilliseconds() const;
 
 		/** Returns time in microseconds since timer was initialized or last reset. */
-		unsigned long getMicroseconds();
-
-		/** Returns time in milliseconds since timer was initialized or last reset. Only CPU timer measured. */
-		unsigned long getMillisecondsCPU();
-
-		/** Returns time in microseconds since timer was initialized or last reset. Only CPU timer measured. */
-		unsigned long getMicrosecondsCPU();
+		UINT64 getMicroseconds() const;
 
 		/**
 		 * Returns the time at which the timer was initialized, in milliseconds.
 		 *
 		 * @return	Time in milliseconds.
 		 */
-		unsigned long getStartMs() const;
+		UINT64 getStartMs() const;
 
 	private:
-		struct Data;
-		Data* m;
+		std::chrono::high_resolution_clock mHRClock;
+		std::chrono::steady_clock::time_point mStartTime;
     };
 
 	/** @} */

+ 2 - 2
Source/BansheeUtility/Source/BsTime.cpp

@@ -12,7 +12,7 @@ namespace BansheeEngine
 		mCurrentFrame(0UL)
 	{
 		mTimer = bs_new<Timer>();
-		mAppStartTime = (UINT64)mTimer->getStartMs();
+		mAppStartTime = mTimer->getStartMs();
 		mLastFrameTime = mTimer->getMicroseconds();
 	}
 
@@ -23,7 +23,7 @@ namespace BansheeEngine
 
 	void Time::_update()
 	{
-		unsigned long currentFrameTime = mTimer->getMicroseconds();
+		UINT64 currentFrameTime = mTimer->getMicroseconds();
 
 		mFrameDelta = (float)((currentFrameTime - mLastFrameTime) * MICROSEC_TO_SEC);
 		mTimeSinceStart = (float)(currentFrameTime * MICROSEC_TO_SEC);

+ 44 - 0
Source/BansheeUtility/Source/BsTimer.cpp

@@ -0,0 +1,44 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsTimer.h"
+#include "BsBitwise.h"
+
+#include <chrono>
+
+using namespace std::chrono;
+
+namespace BansheeEngine
+{
+	Timer::Timer()
+	{
+		reset();
+	}
+
+	void Timer::reset()
+	{
+		mStartTime = mHRClock.now();
+	}
+
+	UINT64 Timer::getMilliseconds() const
+	{
+		steady_clock::time_point newTime = mHRClock.now();
+		nanoseconds newTimeNs = newTime.time_since_epoch();
+
+		return duration_cast<milliseconds>(newTimeNs).count();
+	}
+
+	UINT64 Timer::getMicroseconds() const
+	{
+		steady_clock::time_point newTime = mHRClock.now();
+		nanoseconds newTimeNs = newTime.time_since_epoch();
+
+		return duration_cast<microseconds>(newTimeNs).count();
+	}
+
+	UINT64 Timer::getStartMs() const
+	{
+		nanoseconds startTimeNs = mStartTime.time_since_epoch();
+
+		return duration_cast<milliseconds>(startTimeNs).count();
+	}
+}

+ 0 - 12
Source/BansheeUtility/Source/Win32/BsWin32PlatformUtility.cpp

@@ -16,18 +16,6 @@ namespace BansheeEngine
 			TerminateProcess(GetCurrentProcess(), 0);
 	}
 
-	double PlatformUtility::queryPerformanceTimerMs()
-	{
-		LARGE_INTEGER counterValue;
-		QueryPerformanceCounter(&counterValue);
-
-		LARGE_INTEGER counterFreq;
-		QueryPerformanceFrequency(&counterFreq);
-
-		return (double)counterValue.QuadPart / (counterFreq.QuadPart * 0.001);
-	}
-
-
 	void PlatformUtility::copyToClipboard(const WString& string)
 	{
 		HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));

+ 0 - 88
Source/BansheeUtility/Source/Win32/BsWin32Timer.cpp

@@ -1,88 +0,0 @@
-//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
-//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
-#include "BsTimer.h"
-#include "BsBitwise.h"
-
-#ifndef WIN32_LEAN_AND_MEAN
-#	define WIN32_LEAN_AND_MEAN
-#endif
-#if !defined(NOMINMAX) && defined(_MSC_VER)
-#	define NOMINMAX // Required to stop windows.h messing up std::min
-#endif
-#include "windows.h"
-
-namespace BansheeEngine
-{
-	struct Timer::Data
-	{
-		clock_t zeroClock;
-
-		LARGE_INTEGER startTime;
-		LARGE_INTEGER frequency;
-	};
-
-	Timer::Timer()
-	{
-		m = bs_new<Data>();
-		reset();
-	}
-
-	Timer::~Timer()
-	{
-		bs_delete(m);
-	}
-
-	void Timer::reset()
-	{
-		QueryPerformanceFrequency(&m->frequency);
-		QueryPerformanceCounter(&m->startTime);
-
-		m->zeroClock = clock();
-	}
-
-	unsigned long Timer::getMilliseconds()
-	{
-		LARGE_INTEGER curTime;
-		QueryPerformanceCounter(&curTime);
-
-		LONGLONG newTime = curTime.QuadPart - m->startTime.QuadPart;
-    
-		// Scale by 1000 for milliseconds
-		unsigned long newTicks = (unsigned long)(1000 * newTime / m->frequency.QuadPart);
-
-		return newTicks;
-	}
-
-	unsigned long Timer::getStartMs() const
-	{
-		unsigned long newTicks = (unsigned long)(1000 * m->startTime.QuadPart / m->frequency.QuadPart);
-
-		return newTicks;
-	}
-
-	unsigned long Timer::getMicroseconds()
-	{
-		LARGE_INTEGER curTime;
-		QueryPerformanceCounter(&curTime);
-
-		LONGLONG newTime = curTime.QuadPart - m->startTime.QuadPart;
-    
-		// Scale by 1000000 for microseconds
-		unsigned long newMicro = (unsigned long)(1000000 * newTime / m->frequency.QuadPart);
-
-		return newMicro;
-	}
-
-	unsigned long Timer::getMillisecondsCPU()
-	{
-		clock_t newClock = clock();
-		return (unsigned long)((float)(newClock - m->zeroClock) / ((float)CLOCKS_PER_SEC / 1000.0f));
-	}
-
-
-	unsigned long Timer::getMicrosecondsCPU()
-	{
-		clock_t newClock = clock();
-		return (unsigned long)((float)(newClock - m->zeroClock) / ((float)CLOCKS_PER_SEC / 1000000.0f));
-	}
-}