TickCounter.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Core/TickCounter.h>
  6. #if defined(JPH_PLATFORM_WINDOWS)
  7. JPH_SUPPRESS_WARNING_PUSH
  8. JPH_MSVC_SUPPRESS_WARNING(5039) // winbase.h(13179): warning C5039: 'TpSetCallbackCleanupGroup': pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc. Undefined behavior may occur if this function throws an exception.
  9. #define WIN32_LEAN_AND_MEAN
  10. #ifndef JPH_COMPILER_MINGW
  11. #include <Windows.h>
  12. #else
  13. #include <windows.h>
  14. #endif
  15. JPH_SUPPRESS_WARNING_POP
  16. #endif
  17. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  18. #include <chrono>
  19. JPH_SUPPRESS_WARNINGS_STD_END
  20. JPH_NAMESPACE_BEGIN
  21. static uint64 sReferenceTick;
  22. static std::chrono::high_resolution_clock::time_point sReferenceTime;
  23. #if defined(JPH_PLATFORM_WINDOWS_UWP) || (defined(JPH_PLATFORM_WINDOWS) && defined(JPH_CPU_ARM))
  24. uint64 GetProcessorTickCount()
  25. {
  26. LARGE_INTEGER count;
  27. QueryPerformanceCounter(&count);
  28. return uint64(count.QuadPart);
  29. }
  30. #endif // JPH_PLATFORM_WINDOWS_UWP || (JPH_PLATFORM_WINDOWS && JPH_CPU_ARM)
  31. void UpdateReferenceTime()
  32. {
  33. sReferenceTick = GetProcessorTickCount();
  34. sReferenceTime = std::chrono::high_resolution_clock::now();
  35. }
  36. uint64 GetProcessorTicksPerSecond()
  37. {
  38. uint64 ticks = GetProcessorTickCount();
  39. std::chrono::high_resolution_clock::time_point time = std::chrono::high_resolution_clock::now();
  40. return (ticks - sReferenceTick) * 1000000000ULL / std::chrono::duration_cast<std::chrono::nanoseconds>(time - sReferenceTime).count();
  41. }
  42. JPH_NAMESPACE_END