From 603537719eac6a3391174f10b3b9f0927743d05b Mon Sep 17 00:00:00 2001 From: TellowKrinkle Date: Sat, 11 Sep 2021 02:10:06 -0500 Subject: [PATCH] GS: Switch SW renderer statistics back to rdtsc GetCPUTicks can be pretty low resolution on some systems --- common/emitter/cpudetect.cpp | 15 +++++++++++++++ common/emitter/tools.h | 1 + pcsx2/GS/Renderers/Common/GSFunctionMap.h | 8 ++++---- pcsx2/GS/Renderers/SW/GSRasterizer.cpp | 4 ++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/common/emitter/cpudetect.cpp b/common/emitter/cpudetect.cpp index 6cdf427c9f..fc47fe0af3 100644 --- a/common/emitter/cpudetect.cpp +++ b/common/emitter/cpudetect.cpp @@ -18,6 +18,7 @@ #include "common/emitter/cpudetect_internal.h" #include "common/emitter/internal.h" #include "common/emitter/x86_intrin.h" +#include // CPU information support #if defined(_WIN32) @@ -330,3 +331,17 @@ u32 x86capabilities::CalculateMHz() const else return (u32)(_CPUSpeedHz(span / 500) / 2000); } + +u32 x86capabilities::CachedMHz() +{ + static std::atomic cached{0}; + u32 local = cached.load(std::memory_order_relaxed); + if (unlikely(local == 0)) + { + x86capabilities caps; + caps.Identify(); + local = caps.CalculateMHz(); + cached.store(local, std::memory_order_relaxed); + } + return local; +} diff --git a/common/emitter/tools.h b/common/emitter/tools.h index 93af5c359d..03e07f93f8 100644 --- a/common/emitter/tools.h +++ b/common/emitter/tools.h @@ -116,6 +116,7 @@ public: void CountCores(); wxString GetTypeName() const; + static u32 CachedMHz(); u32 CalculateMHz() const; void SIMD_EstablishMXCSRmask(); diff --git a/pcsx2/GS/Renderers/Common/GSFunctionMap.h b/pcsx2/GS/Renderers/Common/GSFunctionMap.h index a55009f249..81ba4b936b 100644 --- a/pcsx2/GS/Renderers/Common/GSFunctionMap.h +++ b/pcsx2/GS/Renderers/Common/GSFunctionMap.h @@ -19,7 +19,7 @@ #include "GS/GSCodeBuffer.h" #include "GS/Renderers/SW/GSScanlineEnvironment.h" -#include "common/General.h" +#include "common/emitter/tools.h" #include @@ -109,9 +109,9 @@ public: totalTicks += p->ticks; } - double tick_secs = 1.0 / GetTickFrequency(); - double tick_ms = tick_secs * 1000; - double tick_ns = tick_secs * (1000 * 1000 * 1000); + double tick_us = 1.0 / x86capabilities::CachedMHz(); + double tick_ms = tick_us / 1000; + double tick_ns = tick_us * 1000; printf("GS stats\n"); diff --git a/pcsx2/GS/Renderers/SW/GSRasterizer.cpp b/pcsx2/GS/Renderers/SW/GSRasterizer.cpp index 61deaf053f..7fc7d63040 100644 --- a/pcsx2/GS/Renderers/SW/GSRasterizer.cpp +++ b/pcsx2/GS/Renderers/SW/GSRasterizer.cpp @@ -141,7 +141,7 @@ void GSRasterizer::Draw(GSRasterizerData* data) m_pixels.total = 0; m_primcount = 0; - data->start = GetCPUTicks(); + data->start = __rdtsc(); m_ds->BeginDraw(data); @@ -247,7 +247,7 @@ void GSRasterizer::Draw(GSRasterizerData* data) data->pixels = m_pixels.actual; - uint64 ticks = GetCPUTicks() - data->start; + uint64 ticks = __rdtsc() - data->start; m_pixels.sum += m_pixels.actual;