Skip to content

Commit 39dd000

Browse files
committed
QPC usage replaced by std::chrono (and fixed timers)
1 parent 1c05f0a commit 39dd000

File tree

8 files changed

+96
-203
lines changed

8 files changed

+96
-203
lines changed

src/utils/xrLC_Light/LightThread.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ void LightThread::Execute()
2424
gl_data.slots_data.set_slot_calculated(_x, _z);
2525

2626
thProgress = float(_z - Nstart) / float(Nend - Nstart);
27-
thPerformance = float(double(t_count) / double(t_time * CPU::clk_to_seconds)) / 1000.f;
27+
28+
const auto secs = std::chrono::duration_cast<std::chrono::seconds>(t_time).count();
29+
thPerformance = float(double(t_count) / double(secs)) / 1000.f;
2830
}
2931
}
3032
}

src/utils/xrLC_Light/detail_slot_calculate.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class base_color
8383
const int LIGHT_Count = 7;
8484

8585
//-----------------------------------------------------------------
86-
thread_local u64 t_start = 0;
87-
thread_local u64 t_time = 0;
86+
thread_local Time t_start;
87+
thread_local Duration t_time;
8888
thread_local u64 t_count = 0;
8989

9090
IC bool RayPick(CDB::COLLIDER& DB, Fvector& P, Fvector& D, float r, R_Light& L)
@@ -99,9 +99,9 @@ IC bool RayPick(CDB::COLLIDER& DB, Fvector& P, Fvector& D, float r, R_Light& L)
9999
}
100100

101101
// 2. Polygon doesn't pick - real database query
102-
t_start = CPU::GetCLK();
102+
t_start = Clock::now();
103103
DB.ray_query(&gl_data.RCAST_Model, P, D, r);
104-
t_time += CPU::GetCLK() - t_start - CPU::clk_overhead;
104+
t_time += Clock::now() - t_start;
105105
t_count += 1;
106106

107107
// 3. Analyze

src/utils/xrLC_Light/detail_slot_calculate.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#ifndef DETAIL_SLOT_CALCULATE_H_INCLUDED
88
#define DETAIL_SLOT_CALCULATE_H_INCLUDED
99

10+
using Clock = std::chrono::high_resolution_clock;
11+
using Time = Clock::time_point;
12+
using Duration = Clock::duration;
13+
1014
using DWORDVec = xr_vector<u32>;
1115

1216
namespace CDB
@@ -16,7 +20,7 @@ class COLLIDER;
1620
class base_lighting;
1721
struct DetailSlot;
1822

19-
extern thread_local u64 t_time;
23+
extern thread_local Duration t_time;
2024
extern thread_local u64 t_count;
2125

2226
bool detail_slot_calculate(

src/xrCore/FTimer.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33

44
XRCORE_API bool g_bEnableStatGather = false;
55

6-
CStatTimer::CStatTimer()
7-
{
8-
accum = 0;
9-
result = 0.f;
10-
count = 0;
11-
}
12-
136
void CStatTimer::FrameStart()
147
{
15-
accum = 0;
8+
accum = Duration();
169
count = 0;
1710
}
1811

1912
void CStatTimer::FrameEnd()
2013
{
21-
const float time = 1000.f * float(double(accum) / double(CPU::qpc_freq));
14+
const float time = GetElapsed_sec();
2215
if (time > result)
2316
result = time;
2417
else

src/xrCore/FTimer.h

Lines changed: 63 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,48 @@ extern XRCORE_API pauseMngr& g_pauseMngr();
2626

2727
class XRCORE_API CTimerBase
2828
{
29+
public:
30+
using Clock = std::chrono::high_resolution_clock;
31+
using Time = std::chrono::time_point<Clock>;
32+
using Duration = Time::duration;
33+
2934
protected:
30-
u64 startTime;
31-
u64 pauseDuration;
32-
u64 pauseAccum;
35+
Time startTime;
36+
Duration pauseDuration;
37+
Duration pauseAccum;
3338
bool paused;
3439

3540
public:
36-
constexpr CTimerBase() noexcept : startTime(0), pauseDuration(0), pauseAccum(0), paused(false) {}
41+
constexpr CTimerBase() noexcept : startTime(), pauseDuration(), pauseAccum(), paused(false) {}
3742

3843
ICF void Start()
3944
{
4045
if (paused)
4146
return;
42-
startTime = CPU::QPC() - pauseAccum;
47+
startTime = Now() - pauseAccum;
4348
}
44-
ICF u64 GetElapsed_ticks() const
49+
50+
virtual Duration getElapsedTime() const
4551
{
4652
if (paused)
4753
return pauseDuration;
48-
else
49-
return CPU::QPC() - startTime - CPU::qpc_overhead - pauseAccum;
54+
return Now() - startTime - pauseAccum;
55+
}
56+
57+
u64 GetElapsed_ms() const
58+
{
59+
using namespace std::chrono;
60+
return duration_cast<milliseconds>(getElapsedTime()).count();
5061
}
51-
IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); }
62+
5263
IC float GetElapsed_sec() const
5364
{
54-
#ifndef _EDITOR
55-
FPU::m64r();
56-
#endif
57-
float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
58-
#ifndef _EDITOR
59-
FPU::m24r();
60-
#endif
61-
return _result;
65+
using namespace std::chrono;
66+
return duration_cast<duration<float>>(getElapsedTime()).count();
6267
}
68+
69+
Time Now() const { return Clock::now(); }
70+
6371
IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
6472
};
6573

@@ -68,74 +76,49 @@ class XRCORE_API CTimer : public CTimerBase
6876
using inherited = CTimerBase;
6977

7078
float m_time_factor;
71-
u64 m_real_ticks;
72-
u64 m_ticks;
79+
Duration realTime;
80+
Duration time;
7381

74-
IC u64 GetElapsed_ticks(const u64& current_ticks) const
82+
inline Duration getElapsedTime(const Duration& current) const
7583
{
76-
u64 delta = current_ticks - m_real_ticks;
77-
double delta_d = (double)delta;
78-
double time_factor_d = time_factor();
79-
double time = delta_d * time_factor_d + .5;
80-
u64 result = (u64)time;
81-
return (m_ticks + result);
84+
const auto delta = current - realTime;
85+
const double deltaD = double(delta.count());
86+
const double time = deltaD * m_time_factor + .5;
87+
const auto result = u64(time);
88+
return Duration(this->time.count() + result);
8289
}
8390

8491
public:
85-
constexpr CTimer() noexcept : m_time_factor(1.f), m_real_ticks(0), m_ticks(0) {}
86-
ICF void Start() noexcept
92+
constexpr CTimer() noexcept : m_time_factor(1.f), realTime(0), time(0) {}
93+
94+
void Start() noexcept
8795
{
8896
if (paused)
8997
return;
9098

99+
realTime = std::chrono::nanoseconds(0);
100+
time = std::chrono::nanoseconds(0);
91101
inherited::Start();
92-
m_real_ticks = 0;
93-
m_ticks = 0;
94102
}
95103

96104
float time_factor() const noexcept { return m_time_factor; }
97105
void time_factor(const float time_factor) noexcept
98106
{
99-
u64 current = inherited::GetElapsed_ticks();
100-
m_ticks = GetElapsed_ticks(current);
101-
m_real_ticks = current;
107+
const Duration current = inherited::getElapsedTime();
108+
time = getElapsedTime(current);
109+
realTime = current;
102110
m_time_factor = time_factor;
103111
}
104112

105-
u64 GetElapsed_ticks() const
106-
{
107-
#ifndef _EDITOR
108-
FPU::m64r();
109-
#endif // _EDITOR
110-
111-
u64 result = GetElapsed_ticks(inherited::GetElapsed_ticks());
112-
113-
#ifndef _EDITOR
114-
FPU::m24r();
115-
#endif // _EDITOR
116-
117-
return (result);
118-
}
119-
120-
IC u32 GetElapsed_ms() const { return (u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq)); }
121-
IC float GetElapsed_sec() const
113+
virtual Duration getElapsedTime() const
122114
{
123-
#ifndef _EDITOR
124-
FPU::m64r();
125-
#endif
126-
float result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
127-
#ifndef _EDITOR
128-
FPU::m24r();
129-
#endif
130-
return (result);
115+
return getElapsedTime(inherited::getElapsedTime());
131116
}
132-
133-
void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
134117
};
135118

136119
class XRCORE_API CTimer_paused_ex : public CTimer
137120
{
138-
u64 save_clock;
121+
Time save_clock;
139122

140123
public:
141124
CTimer_paused_ex() noexcept : save_clock() {}
@@ -146,15 +129,15 @@ class XRCORE_API CTimer_paused_ex : public CTimer
146129
if (paused == b)
147130
return;
148131

149-
u64 _current = CPU::QPC() - CPU::qpc_overhead;
132+
const auto current = Now();
150133
if (b)
151134
{
152-
save_clock = _current;
153-
pauseDuration = CTimerBase::GetElapsed_ticks();
135+
save_clock = current;
136+
pauseDuration = CTimerBase::getElapsedTime();
154137
}
155138
else
156139
{
157-
pauseAccum += _current - save_clock;
140+
pauseAccum += current - save_clock;
158141
}
159142
paused = b;
160143
}
@@ -170,13 +153,15 @@ class XRCORE_API CTimer_paused : public CTimer_paused_ex
170153
extern XRCORE_API bool g_bEnableStatGather;
171154
class XRCORE_API CStatTimer
172155
{
156+
using Duration = CTimerBase::Duration;
157+
173158
public:
174159
CTimer T;
175-
u64 accum;
160+
Duration accum;
176161
float result;
177162
u32 count;
178163

179-
CStatTimer();
164+
CStatTimer() : T(), accum(), result(.0f), count(0) {}
180165
void FrameStart();
181166
void FrameEnd();
182167

@@ -192,20 +177,19 @@ class XRCORE_API CStatTimer
192177
{
193178
if (!g_bEnableStatGather)
194179
return;
195-
accum += T.GetElapsed_ticks();
180+
accum += T.getElapsedTime();
196181
}
197182

198-
ICF u64 GetElapsed_ticks() const { return accum; }
199-
IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); }
200-
IC float GetElapsed_sec() const
183+
Duration getElapsedTime() const { return accum; }
184+
u64 GetElapsed_ms() const
185+
{
186+
using namespace std::chrono;
187+
return duration_cast<milliseconds>(getElapsedTime()).count();
188+
}
189+
190+
float GetElapsed_sec() const
201191
{
202-
#ifndef _EDITOR
203-
FPU::m64r();
204-
#endif
205-
float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
206-
#ifndef _EDITOR
207-
FPU::m24r();
208-
#endif
209-
return _result;
192+
using namespace std::chrono;
193+
return duration_cast<duration<float>>(getElapsedTime()).count();
210194
}
211195
};

0 commit comments

Comments
 (0)