LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <chrono>
8
11
13
14namespace LkEngine {
15
16 using namespace std::chrono_literals;
17
25 class LTimer
26 {
27 public:
28 LTimer()
29 {
30 Reset();
31 }
32
33 virtual ~LTimer() = default;
34
35 FORCEINLINE void Reset()
36 {
37 using namespace std::chrono;
38 StartTime = high_resolution_clock::now();
39 }
40
41 template<typename TDuration>
42 TDuration GetElapsed() const
43 {
44 using namespace std::chrono;
45 static_assert(std::disjunction_v<
46 std::is_same<TDuration, microseconds>,
47 std::is_same<TDuration, milliseconds>,
48 std::is_same<TDuration, seconds>>, "Timer format not supported");
49 return duration_cast<TDuration>(high_resolution_clock::now() - StartTime);
50 }
51
52 FORCEINLINE float GetDeltaTime()
53 {
54 using namespace std::chrono;
55 const time_point<high_resolution_clock> Now = high_resolution_clock::now();
56 const duration<float, std::milli> DeltaTime = Now - LastTime;
57 LastTime = Now;
58
59 return DeltaTime.count();
60 }
61
62 private:
63 std::chrono::time_point<std::chrono::high_resolution_clock> StartTime{};
64 std::chrono::time_point<std::chrono::high_resolution_clock> LastTime{};
65 };
66
73 {
74 public:
75 FScopedTimer(const std::string& InName)
76 : Name(InName)
77 {
78 }
79
81 {
82 const float Time = Timer.GetElapsed<std::chrono::milliseconds>().count();
83 LK_CORE_INFO_TAG("TIMER", "{0} - {1}ms", Name, Time);
84 }
85
86 private:
87 std::string Name{};
88 LTimer Timer{};
89 };
90
99 {
100 public:
102 {
103 float Time = 0.0f;
104 uint32_t Samples = 0;
105
106 FFrameData() = default;
107 FFrameData(const float InTime) : Time(InTime) {}
108
109 operator float() const { return Time; }
110
111 inline FFrameData& operator+=(const float InTime)
112 {
113 Time += InTime;
114 return *this;
115 }
116 };
117 public:
118 FORCEINLINE void SetFrameTiming(const char* InName, const float InTime)
119 {
120 std::scoped_lock<std::mutex> ScopedLock(FrameDataMutex);
121 if (FrameDataMap.find(InName) == FrameDataMap.end())
122 {
123 FrameDataMap[InName] = 0.0f;
124 }
125
126 FFrameData& FrameData = FrameDataMap[InName];
127 FrameData.Time += InTime;
128 FrameData.Samples++;
129 }
130
131 FORCEINLINE void Clear()
132 {
133 std::scoped_lock<std::mutex> ScopedLock(FrameDataMutex);
134 FrameDataMap.clear();
135 }
136
137 const std::unordered_map<const char*, FFrameData>& GetFrameData() const
138 {
139 return FrameDataMap;
140 }
141
142 private:
143 std::unordered_map<const char*, FFrameData> FrameDataMap{};
144 inline static std::mutex FrameDataMutex;
145 };
146
154 {
155 public:
156 FScopedPerformanceTimer(const char* InName, LPerformanceProfiler* InPerformanceProfiler)
157 : Name(InName)
158 , Profiler(InPerformanceProfiler)
159 {
160 LK_CORE_ASSERT(Profiler, "Profiler is nullptr");
161 }
162
164 {
165 const float Time = Timer.GetElapsed<std::chrono::milliseconds>().count();
166 Profiler->SetFrameTiming(Name, Time);
167 }
168
169 private:
170 const char* Name;
171 LPerformanceProfiler* Profiler{};
172
173 LTimer Timer;
174 };
175
178}
179
Core macros used by the entire engine.
Core types.
Profiling and instrumentation macros.
Definition Timer.h:154
Definition Timer.h:73
Definition Timer.h:99
Definition Timer.h:26
Definition Asset.h:11