Skip to content

Commit f64580e

Browse files
committed
Fixed timers bugs
Revert "Timer: QPC usage replaced by std::chrono" This reverts commit c345b9e.
1 parent 9096480 commit f64580e

File tree

9 files changed

+228
-127
lines changed

9 files changed

+228
-127
lines changed

src/utils/xrLC_Light/LightThread.cpp

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

2626
thProgress = float(_z - Nstart) / float(Nend - Nstart);
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;
27+
thPerformance = float(double(t_count) / double(t_time * CPU::clk_to_seconds)) / 1000.f;
3028
}
3129
}
3230
}

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 Time t_start;
87-
thread_local Duration t_time;
86+
thread_local u64 t_start = 0;
87+
thread_local u64 t_time = 0;
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 = Clock::now();
102+
t_start = CPU::GetCLK();
103103
DB.ray_query(&gl_data.RCAST_Model, P, D, r);
104-
t_time += Clock::now() - t_start;
104+
t_time += CPU::GetCLK() - t_start - CPU::clk_overhead;
105105
t_count += 1;
106106

107107
// 3. Analyze

src/utils/xrLC_Light/detail_slot_calculate.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
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-
1410
using DWORDVec = xr_vector<u32>;
1511

1612
namespace CDB
@@ -20,7 +16,7 @@ class COLLIDER;
2016
class base_lighting;
2117
struct DetailSlot;
2218

23-
extern thread_local Duration t_time;
19+
extern thread_local u64 t_time;
2420
extern thread_local u64 t_count;
2521

2622
bool detail_slot_calculate(

src/xrCore/FTimer.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
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+
613
void CStatTimer::FrameStart()
714
{
8-
accum = Duration();
15+
accum = 0;
916
count = 0;
1017
}
1118

12-
void CStatTimer::FrameEnd() {
13-
14-
const float time = GetElapsed_sec();
19+
void CStatTimer::FrameEnd()
20+
{
21+
const float time = 1000.f * float(double(accum) / double(CPU::qpc_freq));
1522
if (time > result)
1623
result = time;
1724
else
@@ -24,7 +31,7 @@ XRCORE_API pauseMngr& g_pauseMngr()
2431
return manager;
2532
}
2633

27-
pauseMngr::pauseMngr() : paused(FALSE) { m_timers.reserve(3); }
34+
pauseMngr::pauseMngr() : paused(false) { m_timers.reserve(3); }
2835
void pauseMngr::Pause(const bool b)
2936
{
3037
if (paused == b)

src/xrCore/FTimer.h

Lines changed: 97 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
2-
#ifndef FTimerH
3-
#define FTimerH
2+
43
#include "Common/Noncopyable.hpp"
54
#include "_types.h"
65
#include "xrCommon/xr_vector.h"
@@ -27,114 +26,136 @@ extern XRCORE_API pauseMngr& g_pauseMngr();
2726

2827
class XRCORE_API CTimerBase
2928
{
30-
public:
31-
using Clock = std::chrono::high_resolution_clock;
32-
using Time = std::chrono::time_point<Clock>;
33-
using Duration = Time::duration;
34-
3529
protected:
36-
Time startTime;
37-
Duration pauseDuration;
38-
Duration pauseAccum;
30+
u64 startTime;
31+
u64 pauseDuration;
32+
u64 pauseAccum;
3933
bool paused;
4034

4135
public:
42-
constexpr CTimerBase() noexcept : startTime(), pauseDuration(), pauseAccum(), paused(false) {}
36+
constexpr CTimerBase() noexcept : startTime(0), pauseDuration(0), pauseAccum(0), paused(false) {}
4337

44-
void Start()
38+
ICF void Start()
4539
{
46-
if (paused) return;
47-
startTime = Clock::now() - pauseAccum;
48-
}
49-
50-
Duration getElapsedTime() const
51-
{
52-
if (paused) return pauseDuration;
53-
return Clock::now() - startTime - pauseAccum;
40+
if (paused)
41+
return;
42+
startTime = CPU::QPC() - pauseAccum;
5443
}
55-
56-
u64 GetElapsed_ms() const
44+
ICF u64 GetElapsed_ticks() const
5745
{
58-
using namespace std::chrono;
59-
return duration_cast<milliseconds>(getElapsedTime()).count();
46+
if (paused)
47+
return pauseDuration;
48+
else
49+
return CPU::QPC() - startTime - CPU::qpc_overhead - pauseAccum;
6050
}
61-
62-
float GetElapsed_sec() const
51+
IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); }
52+
IC float GetElapsed_sec() const
6353
{
64-
using namespace std::chrono;
65-
const auto nanos = duration_cast<nanoseconds>(getElapsedTime()).count();
66-
return float(nanos) / 1000000000.0;
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;
6762
}
68-
69-
void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
63+
IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
7064
};
7165

7266
class XRCORE_API CTimer : public CTimerBase
7367
{
74-
using super = CTimerBase;
68+
using inherited = CTimerBase;
7569

7670
float m_time_factor;
77-
Duration realTime;
78-
Duration time;
71+
u64 m_real_ticks;
72+
u64 m_ticks;
7973

80-
Duration getElapsedTime(const Duration current) const
74+
IC u64 GetElapsed_ticks(const u64& current_ticks) const
8175
{
82-
const auto delta = current - realTime;
83-
const auto deltaD = double(delta.count());
84-
const auto time_factor_d = double(time_factor());
85-
const double time = deltaD * time_factor_d + .5;
86-
const auto result = u64(time);
87-
return Duration(result);
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);
8882
}
8983

9084
public:
91-
constexpr CTimer() noexcept : m_time_factor(1.f), realTime(), time() {}
92-
93-
void Start() noexcept
85+
constexpr CTimer() noexcept : m_time_factor(1.f), m_real_ticks(0), m_ticks(0) {}
86+
ICF void Start() noexcept
9487
{
95-
if (paused) return;
88+
if (paused)
89+
return;
9690

97-
super::Start();
91+
inherited::Start();
92+
m_real_ticks = 0;
93+
m_ticks = 0;
9894
}
9995

10096
float time_factor() const noexcept { return m_time_factor; }
101-
10297
void time_factor(const float time_factor) noexcept
10398
{
104-
const auto current = super::getElapsedTime();
105-
time = getElapsedTime(current);
106-
realTime = current;
99+
u64 current = inherited::GetElapsed_ticks();
100+
m_ticks = GetElapsed_ticks(current);
101+
m_real_ticks = current;
107102
m_time_factor = time_factor;
108103
}
109104

110-
Duration getElapsedTime() const
105+
u64 GetElapsed_ticks() const
111106
{
112-
return super::getElapsedTime();
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);
113118
}
119+
120+
IC u32 GetElapsed_ms() const { return (u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq)); }
121+
IC float GetElapsed_sec() const
122+
{
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);
131+
}
132+
133+
void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
114134
};
115135

116136
class XRCORE_API CTimer_paused_ex : public CTimer
117137
{
118-
Time save_clock;
138+
u64 save_clock;
119139

120140
public:
121141
CTimer_paused_ex() noexcept : save_clock() {}
122142
virtual ~CTimer_paused_ex() {}
123143
bool Paused() const noexcept { return paused; }
124-
125144
void Pause(const bool b) noexcept
126145
{
127-
if (paused == b) return;
146+
if (paused == b)
147+
return;
128148

129-
const auto current = Clock::now();
149+
u64 _current = CPU::QPC() - CPU::qpc_overhead;
130150
if (b)
131151
{
132-
save_clock = current;
133-
pauseDuration = CTimerBase::getElapsedTime();
152+
save_clock = _current;
153+
pauseDuration = CTimerBase::GetElapsed_ticks();
134154
}
135155
else
136-
pauseAccum += current - save_clock;
137-
156+
{
157+
pauseAccum += _current - save_clock;
158+
}
138159
paused = b;
139160
}
140161
};
@@ -147,50 +168,44 @@ class XRCORE_API CTimer_paused : public CTimer_paused_ex
147168
};
148169

149170
extern XRCORE_API bool g_bEnableStatGather;
150-
151171
class XRCORE_API CStatTimer
152172
{
153-
using Duration = CTimerBase::Duration;
154-
155173
public:
156174
CTimer T;
157-
Duration accum;
175+
u64 accum;
158176
float result;
159177
u32 count;
160178

161-
CStatTimer() : T(), accum(), result(.0f), count(0) {}
179+
CStatTimer();
162180
void FrameStart();
163181
void FrameEnd();
164182

165-
void Begin()
183+
ICF void Begin()
166184
{
167185
if (!g_bEnableStatGather)
168186
return;
169187
count++;
170188
T.Start();
171189
}
172190

173-
void End()
191+
ICF void End()
174192
{
175193
if (!g_bEnableStatGather)
176194
return;
177-
accum += T.getElapsedTime();
178-
}
179-
180-
Duration getElapsedTime() const { return accum; }
181-
182-
u64 GetElapsed_ms() const
183-
{
184-
using namespace std::chrono;
185-
return duration_cast<milliseconds>(getElapsedTime()).count();
195+
accum += T.GetElapsed_ticks();
186196
}
187197

188-
float GetElapsed_sec() const
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
189201
{
190-
using namespace std::chrono;
191-
const auto nanos = duration_cast<nanoseconds>(getElapsedTime()).count();
192-
return float(nanos) / 1000000000.0;
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;
193210
}
194211
};
195-
196-
#endif // FTimerH

0 commit comments

Comments
 (0)