Skip to content

Commit

Permalink
QPC usage replaced by std::chrono (and fixed timers)
Browse files Browse the repository at this point in the history
  • Loading branch information
qweasdd136963 committed Sep 29, 2018
1 parent 1c05f0a commit 39dd000
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 203 deletions.
4 changes: 3 additions & 1 deletion src/utils/xrLC_Light/LightThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ void LightThread::Execute()
gl_data.slots_data.set_slot_calculated(_x, _z);

thProgress = float(_z - Nstart) / float(Nend - Nstart);
thPerformance = float(double(t_count) / double(t_time * CPU::clk_to_seconds)) / 1000.f;

const auto secs = std::chrono::duration_cast<std::chrono::seconds>(t_time).count();
thPerformance = float(double(t_count) / double(secs)) / 1000.f;
}
}
}
8 changes: 4 additions & 4 deletions src/utils/xrLC_Light/detail_slot_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class base_color
const int LIGHT_Count = 7;

//-----------------------------------------------------------------
thread_local u64 t_start = 0;
thread_local u64 t_time = 0;
thread_local Time t_start;
thread_local Duration t_time;
thread_local u64 t_count = 0;

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

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

// 3. Analyze
Expand Down
6 changes: 5 additions & 1 deletion src/utils/xrLC_Light/detail_slot_calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#ifndef DETAIL_SLOT_CALCULATE_H_INCLUDED
#define DETAIL_SLOT_CALCULATE_H_INCLUDED

using Clock = std::chrono::high_resolution_clock;
using Time = Clock::time_point;
using Duration = Clock::duration;

using DWORDVec = xr_vector<u32>;

namespace CDB
Expand All @@ -16,7 +20,7 @@ class COLLIDER;
class base_lighting;
struct DetailSlot;

extern thread_local u64 t_time;
extern thread_local Duration t_time;
extern thread_local u64 t_count;

bool detail_slot_calculate(
Expand Down
11 changes: 2 additions & 9 deletions src/xrCore/FTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@

XRCORE_API bool g_bEnableStatGather = false;

CStatTimer::CStatTimer()
{
accum = 0;
result = 0.f;
count = 0;
}

void CStatTimer::FrameStart()
{
accum = 0;
accum = Duration();
count = 0;
}

void CStatTimer::FrameEnd()
{
const float time = 1000.f * float(double(accum) / double(CPU::qpc_freq));
const float time = GetElapsed_sec();
if (time > result)
result = time;
else
Expand Down
142 changes: 63 additions & 79 deletions src/xrCore/FTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,48 @@ extern XRCORE_API pauseMngr& g_pauseMngr();

class XRCORE_API CTimerBase
{
public:
using Clock = std::chrono::high_resolution_clock;
using Time = std::chrono::time_point<Clock>;
using Duration = Time::duration;

protected:
u64 startTime;
u64 pauseDuration;
u64 pauseAccum;
Time startTime;
Duration pauseDuration;
Duration pauseAccum;
bool paused;

public:
constexpr CTimerBase() noexcept : startTime(0), pauseDuration(0), pauseAccum(0), paused(false) {}
constexpr CTimerBase() noexcept : startTime(), pauseDuration(), pauseAccum(), paused(false) {}

ICF void Start()
{
if (paused)
return;
startTime = CPU::QPC() - pauseAccum;
startTime = Now() - pauseAccum;
}
ICF u64 GetElapsed_ticks() const

virtual Duration getElapsedTime() const
{
if (paused)
return pauseDuration;
else
return CPU::QPC() - startTime - CPU::qpc_overhead - pauseAccum;
return Now() - startTime - pauseAccum;
}

u64 GetElapsed_ms() const
{
using namespace std::chrono;
return duration_cast<milliseconds>(getElapsedTime()).count();
}
IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); }

IC float GetElapsed_sec() const
{
#ifndef _EDITOR
FPU::m64r();
#endif
float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
#ifndef _EDITOR
FPU::m24r();
#endif
return _result;
using namespace std::chrono;
return duration_cast<duration<float>>(getElapsedTime()).count();
}

Time Now() const { return Clock::now(); }

IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
};

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

float m_time_factor;
u64 m_real_ticks;
u64 m_ticks;
Duration realTime;
Duration time;

IC u64 GetElapsed_ticks(const u64& current_ticks) const
inline Duration getElapsedTime(const Duration& current) const
{
u64 delta = current_ticks - m_real_ticks;
double delta_d = (double)delta;
double time_factor_d = time_factor();
double time = delta_d * time_factor_d + .5;
u64 result = (u64)time;
return (m_ticks + result);
const auto delta = current - realTime;
const double deltaD = double(delta.count());
const double time = deltaD * m_time_factor + .5;
const auto result = u64(time);
return Duration(this->time.count() + result);
}

public:
constexpr CTimer() noexcept : m_time_factor(1.f), m_real_ticks(0), m_ticks(0) {}
ICF void Start() noexcept
constexpr CTimer() noexcept : m_time_factor(1.f), realTime(0), time(0) {}

void Start() noexcept
{
if (paused)
return;

realTime = std::chrono::nanoseconds(0);
time = std::chrono::nanoseconds(0);
inherited::Start();
m_real_ticks = 0;
m_ticks = 0;
}

float time_factor() const noexcept { return m_time_factor; }
void time_factor(const float time_factor) noexcept
{
u64 current = inherited::GetElapsed_ticks();
m_ticks = GetElapsed_ticks(current);
m_real_ticks = current;
const Duration current = inherited::getElapsedTime();
time = getElapsedTime(current);
realTime = current;
m_time_factor = time_factor;
}

u64 GetElapsed_ticks() const
{
#ifndef _EDITOR
FPU::m64r();
#endif // _EDITOR

u64 result = GetElapsed_ticks(inherited::GetElapsed_ticks());

#ifndef _EDITOR
FPU::m24r();
#endif // _EDITOR

return (result);
}

IC u32 GetElapsed_ms() const { return (u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq)); }
IC float GetElapsed_sec() const
virtual Duration getElapsedTime() const
{
#ifndef _EDITOR
FPU::m64r();
#endif
float result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
#ifndef _EDITOR
FPU::m24r();
#endif
return (result);
return getElapsedTime(inherited::getElapsedTime());
}

void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); }
};

class XRCORE_API CTimer_paused_ex : public CTimer
{
u64 save_clock;
Time save_clock;

public:
CTimer_paused_ex() noexcept : save_clock() {}
Expand All @@ -146,15 +129,15 @@ class XRCORE_API CTimer_paused_ex : public CTimer
if (paused == b)
return;

u64 _current = CPU::QPC() - CPU::qpc_overhead;
const auto current = Now();
if (b)
{
save_clock = _current;
pauseDuration = CTimerBase::GetElapsed_ticks();
save_clock = current;
pauseDuration = CTimerBase::getElapsedTime();
}
else
{
pauseAccum += _current - save_clock;
pauseAccum += current - save_clock;
}
paused = b;
}
Expand All @@ -170,13 +153,15 @@ class XRCORE_API CTimer_paused : public CTimer_paused_ex
extern XRCORE_API bool g_bEnableStatGather;
class XRCORE_API CStatTimer
{
using Duration = CTimerBase::Duration;

public:
CTimer T;
u64 accum;
Duration accum;
float result;
u32 count;

CStatTimer();
CStatTimer() : T(), accum(), result(.0f), count(0) {}
void FrameStart();
void FrameEnd();

Expand All @@ -192,20 +177,19 @@ class XRCORE_API CStatTimer
{
if (!g_bEnableStatGather)
return;
accum += T.GetElapsed_ticks();
accum += T.getElapsedTime();
}

ICF u64 GetElapsed_ticks() const { return accum; }
IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); }
IC float GetElapsed_sec() const
Duration getElapsedTime() const { return accum; }
u64 GetElapsed_ms() const
{
using namespace std::chrono;
return duration_cast<milliseconds>(getElapsedTime()).count();
}

float GetElapsed_sec() const
{
#ifndef _EDITOR
FPU::m64r();
#endif
float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq));
#ifndef _EDITOR
FPU::m24r();
#endif
return _result;
using namespace std::chrono;
return duration_cast<duration<float>>(getElapsedTime()).count();
}
};
Loading

0 comments on commit 39dd000

Please sign in to comment.