summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt13
-rw-r--r--src/common/bit_cast.h20
-rw-r--r--src/common/input.h2
-rw-r--r--src/common/logging/filter.cpp2
-rw-r--r--src/common/logging/types.h202
-rw-r--r--src/common/overflow.h22
-rw-r--r--src/common/scratch_buffer.h1
-rw-r--r--src/common/settings.cpp2
-rw-r--r--src/common/settings.h11
-rw-r--r--src/common/steady_clock.cpp56
-rw-r--r--src/common/steady_clock.h23
-rw-r--r--src/common/wall_clock.cpp39
-rw-r--r--src/common/wall_clock.h3
-rw-r--r--src/common/windows/timer_resolution.cpp109
-rw-r--r--src/common/windows/timer_resolution.h38
-rw-r--r--src/common/x64/native_clock.cpp17
16 files changed, 414 insertions, 146 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 9884a4a0b..61ab68864 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -91,6 +91,7 @@ add_library(common STATIC
multi_level_page_table.h
nvidia_flags.cpp
nvidia_flags.h
+ overflow.h
page_table.cpp
page_table.h
param_package.cpp
@@ -113,6 +114,8 @@ add_library(common STATIC
socket_types.h
spin_lock.cpp
spin_lock.h
+ steady_clock.cpp
+ steady_clock.h
stream.cpp
stream.h
string_util.cpp
@@ -142,6 +145,14 @@ add_library(common STATIC
zstd_compression.h
)
+if (WIN32)
+ target_sources(common PRIVATE
+ windows/timer_resolution.cpp
+ windows/timer_resolution.h
+ )
+ target_link_libraries(common PRIVATE ntdll)
+endif()
+
if(ARCHITECTURE_x86_64)
target_sources(common
PRIVATE
@@ -176,7 +187,7 @@ endif()
create_target_directory_groups(common)
-target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile Threads::Threads)
+target_link_libraries(common PUBLIC Boost::context Boost::headers fmt::fmt microprofile Threads::Threads)
target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd LLVM::Demangle)
if (YUZU_USE_PRECOMPILED_HEADERS)
diff --git a/src/common/bit_cast.h b/src/common/bit_cast.h
index 535148b4d..c6110c542 100644
--- a/src/common/bit_cast.h
+++ b/src/common/bit_cast.h
@@ -3,19 +3,21 @@
#pragma once
-#include <cstring>
-#include <type_traits>
+#include <version>
+
+#ifdef __cpp_lib_bit_cast
+#include <bit>
+#endif
namespace Common {
template <typename To, typename From>
-[[nodiscard]] std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
- std::is_trivially_copyable_v<To>,
- To>
-BitCast(const From& src) noexcept {
- To dst;
- std::memcpy(&dst, &src, sizeof(To));
- return dst;
+constexpr inline To BitCast(const From& from) {
+#ifdef __cpp_lib_bit_cast
+ return std::bit_cast<To>(from);
+#else
+ return __builtin_bit_cast(To, from);
+#endif
}
} // namespace Common
diff --git a/src/common/input.h b/src/common/input.h
index b5748a6c8..98e934685 100644
--- a/src/common/input.h
+++ b/src/common/input.h
@@ -46,7 +46,7 @@ enum class PollingMode {
// Constant polling of buttons, analogs and motion data
Active,
// Only update on button change, digital analogs
- Pasive,
+ Passive,
// Enable near field communication polling
NFC,
// Enable infrared camera polling
diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp
index a959acb74..c95909561 100644
--- a/src/common/logging/filter.cpp
+++ b/src/common/logging/filter.cpp
@@ -119,7 +119,7 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) {
SUB(Service, NPNS) \
SUB(Service, NS) \
SUB(Service, NVDRV) \
- SUB(Service, NVFlinger) \
+ SUB(Service, Nvnflinger) \
SUB(Service, OLSC) \
SUB(Service, PCIE) \
SUB(Service, PCTL) \
diff --git a/src/common/logging/types.h b/src/common/logging/types.h
index 595c15ada..8356e3183 100644
--- a/src/common/logging/types.h
+++ b/src/common/logging/types.h
@@ -29,107 +29,107 @@ enum class Level : u8 {
* filter.cpp.
*/
enum class Class : u8 {
- Log, ///< Messages about the log system itself
- Common, ///< Library routines
- Common_Filesystem, ///< Filesystem interface library
- Common_Memory, ///< Memory mapping and management functions
- Core, ///< LLE emulation core
- Core_ARM, ///< ARM CPU core
- Core_Timing, ///< CoreTiming functions
- Config, ///< Emulator configuration (including commandline)
- Debug, ///< Debugging tools
- Debug_Emulated, ///< Debug messages from the emulated programs
- Debug_GPU, ///< GPU debugging tools
- Debug_Breakpoint, ///< Logging breakpoints and watchpoints
- Debug_GDBStub, ///< GDB Stub
- Kernel, ///< The HLE implementation of the CTR kernel
- Kernel_SVC, ///< Kernel system calls
- Service, ///< HLE implementation of system services. Each major service
- ///< should have its own subclass.
- Service_ACC, ///< The ACC (Accounts) service
- Service_AM, ///< The AM (Applet manager) service
- Service_AOC, ///< The AOC (AddOn Content) service
- Service_APM, ///< The APM (Performance) service
- Service_ARP, ///< The ARP service
- Service_Audio, ///< The Audio (Audio control) service
- Service_BCAT, ///< The BCAT service
- Service_BGTC, ///< The BGTC (Background Task Controller) service
- Service_BPC, ///< The BPC service
- Service_BTDRV, ///< The Bluetooth driver service
- Service_BTM, ///< The BTM service
- Service_Capture, ///< The capture service
- Service_ERPT, ///< The error reporting service
- Service_ETicket, ///< The ETicket service
- Service_EUPLD, ///< The error upload service
- Service_Fatal, ///< The Fatal service
- Service_FGM, ///< The FGM service
- Service_Friend, ///< The friend service
- Service_FS, ///< The FS (Filesystem) service
- Service_GRC, ///< The game recording service
- Service_HID, ///< The HID (Human interface device) service
- Service_IRS, ///< The IRS service
- Service_JIT, ///< The JIT service
- Service_LBL, ///< The LBL (LCD backlight) service
- Service_LDN, ///< The LDN (Local domain network) service
- Service_LDR, ///< The loader service
- Service_LM, ///< The LM (Logger) service
- Service_Migration, ///< The migration service
- Service_Mii, ///< The Mii service
- Service_MM, ///< The MM (Multimedia) service
- Service_MNPP, ///< The MNPP service
- Service_NCM, ///< The NCM service
- Service_NFC, ///< The NFC (Near-field communication) service
- Service_NFP, ///< The NFP service
- Service_NGCT, ///< The NGCT (No Good Content for Terra) service
- Service_NIFM, ///< The NIFM (Network interface) service
- Service_NIM, ///< The NIM service
- Service_NOTIF, ///< The NOTIF (Notification) service
- Service_NPNS, ///< The NPNS service
- Service_NS, ///< The NS services
- Service_NVDRV, ///< The NVDRV (Nvidia driver) service
- Service_NVFlinger, ///< The NVFlinger service
- Service_OLSC, ///< The OLSC service
- Service_PCIE, ///< The PCIe service
- Service_PCTL, ///< The PCTL (Parental control) service
- Service_PCV, ///< The PCV service
- Service_PM, ///< The PM service
- Service_PREPO, ///< The PREPO (Play report) service
- Service_PSC, ///< The PSC service
- Service_PTM, ///< The PTM service
- Service_SET, ///< The SET (Settings) service
- Service_SM, ///< The SM (Service manager) service
- Service_SPL, ///< The SPL service
- Service_SSL, ///< The SSL service
- Service_TCAP, ///< The TCAP service.
- Service_Time, ///< The time service
- Service_USB, ///< The USB (Universal Serial Bus) service
- Service_VI, ///< The VI (Video interface) service
- Service_WLAN, ///< The WLAN (Wireless local area network) service
- HW, ///< Low-level hardware emulation
- HW_Memory, ///< Memory-map and address translation
- HW_LCD, ///< LCD register emulation
- HW_GPU, ///< GPU control emulation
- HW_AES, ///< AES engine emulation
- IPC, ///< IPC interface
- Frontend, ///< Emulator UI
- Render, ///< Emulator video output and hardware acceleration
- Render_Software, ///< Software renderer backend
- Render_OpenGL, ///< OpenGL backend
- Render_Vulkan, ///< Vulkan backend
- Shader, ///< Shader recompiler
- Shader_SPIRV, ///< Shader SPIR-V code generation
- Shader_GLASM, ///< Shader GLASM code generation
- Shader_GLSL, ///< Shader GLSL code generation
- Audio, ///< Audio emulation
- Audio_DSP, ///< The HLE implementation of the DSP
- Audio_Sink, ///< Emulator audio output backend
- Loader, ///< ROM loader
- CheatEngine, ///< Memory manipulation and engine VM functions
- Crypto, ///< Cryptographic engine/functions
- Input, ///< Input emulation
- Network, ///< Network emulation
- WebService, ///< Interface to yuzu Web Services
- Count ///< Total number of logging classes
+ Log, ///< Messages about the log system itself
+ Common, ///< Library routines
+ Common_Filesystem, ///< Filesystem interface library
+ Common_Memory, ///< Memory mapping and management functions
+ Core, ///< LLE emulation core
+ Core_ARM, ///< ARM CPU core
+ Core_Timing, ///< CoreTiming functions
+ Config, ///< Emulator configuration (including commandline)
+ Debug, ///< Debugging tools
+ Debug_Emulated, ///< Debug messages from the emulated programs
+ Debug_GPU, ///< GPU debugging tools
+ Debug_Breakpoint, ///< Logging breakpoints and watchpoints
+ Debug_GDBStub, ///< GDB Stub
+ Kernel, ///< The HLE implementation of the CTR kernel
+ Kernel_SVC, ///< Kernel system calls
+ Service, ///< HLE implementation of system services. Each major service
+ ///< should have its own subclass.
+ Service_ACC, ///< The ACC (Accounts) service
+ Service_AM, ///< The AM (Applet manager) service
+ Service_AOC, ///< The AOC (AddOn Content) service
+ Service_APM, ///< The APM (Performance) service
+ Service_ARP, ///< The ARP service
+ Service_Audio, ///< The Audio (Audio control) service
+ Service_BCAT, ///< The BCAT service
+ Service_BGTC, ///< The BGTC (Background Task Controller) service
+ Service_BPC, ///< The BPC service
+ Service_BTDRV, ///< The Bluetooth driver service
+ Service_BTM, ///< The BTM service
+ Service_Capture, ///< The capture service
+ Service_ERPT, ///< The error reporting service
+ Service_ETicket, ///< The ETicket service
+ Service_EUPLD, ///< The error upload service
+ Service_Fatal, ///< The Fatal service
+ Service_FGM, ///< The FGM service
+ Service_Friend, ///< The friend service
+ Service_FS, ///< The FS (Filesystem) service
+ Service_GRC, ///< The game recording service
+ Service_HID, ///< The HID (Human interface device) service
+ Service_IRS, ///< The IRS service
+ Service_JIT, ///< The JIT service
+ Service_LBL, ///< The LBL (LCD backlight) service
+ Service_LDN, ///< The LDN (Local domain network) service
+ Service_LDR, ///< The loader service
+ Service_LM, ///< The LM (Logger) service
+ Service_Migration, ///< The migration service
+ Service_Mii, ///< The Mii service
+ Service_MM, ///< The MM (Multimedia) service
+ Service_MNPP, ///< The MNPP service
+ Service_NCM, ///< The NCM service
+ Service_NFC, ///< The NFC (Near-field communication) service
+ Service_NFP, ///< The NFP service
+ Service_NGCT, ///< The NGCT (No Good Content for Terra) service
+ Service_NIFM, ///< The NIFM (Network interface) service
+ Service_NIM, ///< The NIM service
+ Service_NOTIF, ///< The NOTIF (Notification) service
+ Service_NPNS, ///< The NPNS service
+ Service_NS, ///< The NS services
+ Service_NVDRV, ///< The NVDRV (Nvidia driver) service
+ Service_Nvnflinger, ///< The Nvnflinger service
+ Service_OLSC, ///< The OLSC service
+ Service_PCIE, ///< The PCIe service
+ Service_PCTL, ///< The PCTL (Parental control) service
+ Service_PCV, ///< The PCV service
+ Service_PM, ///< The PM service
+ Service_PREPO, ///< The PREPO (Play report) service
+ Service_PSC, ///< The PSC service
+ Service_PTM, ///< The PTM service
+ Service_SET, ///< The SET (Settings) service
+ Service_SM, ///< The SM (Service manager) service
+ Service_SPL, ///< The SPL service
+ Service_SSL, ///< The SSL service
+ Service_TCAP, ///< The TCAP service.
+ Service_Time, ///< The time service
+ Service_USB, ///< The USB (Universal Serial Bus) service
+ Service_VI, ///< The VI (Video interface) service
+ Service_WLAN, ///< The WLAN (Wireless local area network) service
+ HW, ///< Low-level hardware emulation
+ HW_Memory, ///< Memory-map and address translation
+ HW_LCD, ///< LCD register emulation
+ HW_GPU, ///< GPU control emulation
+ HW_AES, ///< AES engine emulation
+ IPC, ///< IPC interface
+ Frontend, ///< Emulator UI
+ Render, ///< Emulator video output and hardware acceleration
+ Render_Software, ///< Software renderer backend
+ Render_OpenGL, ///< OpenGL backend
+ Render_Vulkan, ///< Vulkan backend
+ Shader, ///< Shader recompiler
+ Shader_SPIRV, ///< Shader SPIR-V code generation
+ Shader_GLASM, ///< Shader GLASM code generation
+ Shader_GLSL, ///< Shader GLSL code generation
+ Audio, ///< Audio emulation
+ Audio_DSP, ///< The HLE implementation of the DSP
+ Audio_Sink, ///< Emulator audio output backend
+ Loader, ///< ROM loader
+ CheatEngine, ///< Memory manipulation and engine VM functions
+ Crypto, ///< Cryptographic engine/functions
+ Input, ///< Input emulation
+ Network, ///< Network emulation
+ WebService, ///< Interface to yuzu Web Services
+ Count ///< Total number of logging classes
};
} // namespace Common::Log
diff --git a/src/common/overflow.h b/src/common/overflow.h
new file mode 100644
index 000000000..44d8e7e73
--- /dev/null
+++ b/src/common/overflow.h
@@ -0,0 +1,22 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <type_traits>
+#include "bit_cast.h"
+
+namespace Common {
+
+template <typename T>
+ requires(std::is_integral_v<T> && std::is_signed_v<T>)
+inline T WrappingAdd(T lhs, T rhs) {
+ using U = std::make_unsigned_t<T>;
+
+ U lhs_u = BitCast<U>(lhs);
+ U rhs_u = BitCast<U>(rhs);
+
+ return BitCast<T>(lhs_u + rhs_u);
+}
+
+} // namespace Common
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h
index 1245a5086..26d4e76dc 100644
--- a/src/common/scratch_buffer.h
+++ b/src/common/scratch_buffer.h
@@ -23,6 +23,7 @@ public:
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
~ScratchBuffer() = default;
+ ScratchBuffer(ScratchBuffer&&) = default;
/// This will only grow the buffer's capacity if size is greater than the current capacity.
/// The previously held data will remain intact.
diff --git a/src/common/settings.cpp b/src/common/settings.cpp
index 749ac213f..84955030b 100644
--- a/src/common/settings.cpp
+++ b/src/common/settings.cpp
@@ -59,6 +59,7 @@ void LogSettings() {
values.use_asynchronous_gpu_emulation.GetValue());
log_setting("Renderer_NvdecEmulation", values.nvdec_emulation.GetValue());
log_setting("Renderer_AccelerateASTC", values.accelerate_astc.GetValue());
+ log_setting("Renderer_AsyncASTC", values.async_astc.GetValue());
log_setting("Renderer_UseVsync", values.use_vsync.GetValue());
log_setting("Renderer_ShaderBackend", values.shader_backend.GetValue());
log_setting("Renderer_UseAsynchronousShaders", values.use_asynchronous_shaders.GetValue());
@@ -219,6 +220,7 @@ void RestoreGlobalState(bool is_powered_on) {
values.use_asynchronous_gpu_emulation.SetGlobal(true);
values.nvdec_emulation.SetGlobal(true);
values.accelerate_astc.SetGlobal(true);
+ values.async_astc.SetGlobal(true);
values.use_vsync.SetGlobal(true);
values.shader_backend.SetGlobal(true);
values.use_asynchronous_shaders.SetGlobal(true);
diff --git a/src/common/settings.h b/src/common/settings.h
index 6d27dd5ee..b77a1580a 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -128,7 +128,7 @@ public:
/**
* Sets a default value, label, and setting value.
*
- * @param default_val Intial value of the setting, and default value of the setting
+ * @param default_val Initial value of the setting, and default value of the setting
* @param name Label for the setting
*/
explicit Setting(const Type& default_val, const std::string& name)
@@ -139,7 +139,7 @@ public:
/**
* Sets a default value, minimum value, maximum value, and label.
*
- * @param default_val Intial value of the setting, and default value of the setting
+ * @param default_val Initial value of the setting, and default value of the setting
* @param min_val Sets the minimum allowed value of the setting
* @param max_val Sets the maximum allowed value of the setting
* @param name Label for the setting
@@ -231,7 +231,7 @@ public:
/**
* Sets a default value, label, and setting value.
*
- * @param default_val Intial value of the setting, and default value of the setting
+ * @param default_val Initial value of the setting, and default value of the setting
* @param name Label for the setting
*/
explicit SwitchableSetting(const Type& default_val, const std::string& name)
@@ -242,7 +242,7 @@ public:
/**
* Sets a default value, minimum value, maximum value, and label.
*
- * @param default_val Intial value of the setting, and default value of the setting
+ * @param default_val Initial value of the setting, and default value of the setting
* @param min_val Sets the minimum allowed value of the setting
* @param max_val Sets the maximum allowed value of the setting
* @param name Label for the setting
@@ -453,6 +453,7 @@ struct Values {
SwitchableSetting<bool> use_asynchronous_gpu_emulation{true, "use_asynchronous_gpu_emulation"};
SwitchableSetting<NvdecEmulation> nvdec_emulation{NvdecEmulation::GPU, "nvdec_emulation"};
SwitchableSetting<bool> accelerate_astc{true, "accelerate_astc"};
+ SwitchableSetting<bool> async_astc{false, "async_astc"};
SwitchableSetting<bool> use_vsync{true, "use_vsync"};
SwitchableSetting<ShaderBackend, true> shader_backend{ShaderBackend::GLSL, ShaderBackend::GLSL,
ShaderBackend::SPIRV, "shader_backend"};
@@ -502,7 +503,7 @@ struct Values {
Setting<bool> tas_loop{false, "tas_loop"};
Setting<bool> mouse_panning{false, "mouse_panning"};
- Setting<u8, true> mouse_panning_sensitivity{10, 1, 100, "mouse_panning_sensitivity"};
+ Setting<u8, true> mouse_panning_sensitivity{50, 1, 100, "mouse_panning_sensitivity"};
Setting<bool> mouse_enabled{false, "mouse_enabled"};
Setting<bool> emulate_analog_keyboard{false, "emulate_analog_keyboard"};
diff --git a/src/common/steady_clock.cpp b/src/common/steady_clock.cpp
new file mode 100644
index 000000000..0d5908aa7
--- /dev/null
+++ b/src/common/steady_clock.cpp
@@ -0,0 +1,56 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#if defined(_WIN32)
+#include <windows.h>
+#else
+#include <time.h>
+#endif
+
+#include "common/steady_clock.h"
+
+namespace Common {
+
+#ifdef _WIN32
+static s64 WindowsQueryPerformanceFrequency() {
+ LARGE_INTEGER frequency;
+ QueryPerformanceFrequency(&frequency);
+ return frequency.QuadPart;
+}
+
+static s64 WindowsQueryPerformanceCounter() {
+ LARGE_INTEGER counter;
+ QueryPerformanceCounter(&counter);
+ return counter.QuadPart;
+}
+#endif
+
+SteadyClock::time_point SteadyClock::Now() noexcept {
+#if defined(_WIN32)
+ static const auto freq = WindowsQueryPerformanceFrequency();
+ const auto counter = WindowsQueryPerformanceCounter();
+
+ // 10 MHz is a very common QPC frequency on modern PCs.
+ // Optimizing for this specific frequency can double the performance of
+ // this function by avoiding the expensive frequency conversion path.
+ static constexpr s64 TenMHz = 10'000'000;
+
+ if (freq == TenMHz) [[likely]] {
+ static_assert(period::den % TenMHz == 0);
+ static constexpr s64 Multiplier = period::den / TenMHz;
+ return time_point{duration{counter * Multiplier}};
+ }
+
+ const auto whole = (counter / freq) * period::den;
+ const auto part = (counter % freq) * period::den / freq;
+ return time_point{duration{whole + part}};
+#elif defined(__APPLE__)
+ return time_point{duration{clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW)}};
+#else
+ timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}};
+#endif
+}
+
+}; // namespace Common
diff --git a/src/common/steady_clock.h b/src/common/steady_clock.h
new file mode 100644
index 000000000..9497cf865
--- /dev/null
+++ b/src/common/steady_clock.h
@@ -0,0 +1,23 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <chrono>
+
+#include "common/common_types.h"
+
+namespace Common {
+
+struct SteadyClock {
+ using rep = s64;
+ using period = std::nano;
+ using duration = std::chrono::nanoseconds;
+ using time_point = std::chrono::time_point<SteadyClock>;
+
+ static constexpr bool is_steady = true;
+
+ [[nodiscard]] static time_point Now() noexcept;
+};
+
+} // namespace Common
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp
index ae07f2811..817e71d52 100644
--- a/src/common/wall_clock.cpp
+++ b/src/common/wall_clock.cpp
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
+#include "common/steady_clock.h"
#include "common/uint128.h"
#include "common/wall_clock.h"
@@ -11,45 +12,32 @@
namespace Common {
-using base_timer = std::chrono::steady_clock;
-using base_time_point = std::chrono::time_point<base_timer>;
-
class StandardWallClock final : public WallClock {
public:
explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_)
- : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) {
- start_time = base_timer::now();
- }
+ : WallClock{emulated_cpu_frequency_, emulated_clock_frequency_, false},
+ start_time{SteadyClock::Now()} {}
std::chrono::nanoseconds GetTimeNS() override {
- base_time_point current = base_timer::now();
- auto elapsed = current - start_time;
- return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
+ return SteadyClock::Now() - start_time;
}
std::chrono::microseconds GetTimeUS() override {
- base_time_point current = base_timer::now();
- auto elapsed = current - start_time;
- return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
+ return std::chrono::duration_cast<std::chrono::microseconds>(GetTimeNS());
}
std::chrono::milliseconds GetTimeMS() override {
- base_time_point current = base_timer::now();
- auto elapsed = current - start_time;
- return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
+ return std::chrono::duration_cast<std::chrono::milliseconds>(GetTimeNS());
}
u64 GetClockCycles() override {
- std::chrono::nanoseconds time_now = GetTimeNS();
- const u128 temporary =
- Common::Multiply64Into128(time_now.count(), emulated_clock_frequency);
- return Common::Divide128On32(temporary, 1000000000).first;
+ const u128 temp = Common::Multiply64Into128(GetTimeNS().count(), emulated_clock_frequency);
+ return Common::Divide128On32(temp, NS_RATIO).first;
}
u64 GetCPUCycles() override {
- std::chrono::nanoseconds time_now = GetTimeNS();
- const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency);
- return Common::Divide128On32(temporary, 1000000000).first;
+ const u128 temp = Common::Multiply64Into128(GetTimeNS().count(), emulated_cpu_frequency);
+ return Common::Divide128On32(temp, NS_RATIO).first;
}
void Pause([[maybe_unused]] bool is_paused) override {
@@ -57,7 +45,7 @@ public:
}
private:
- base_time_point start_time;
+ SteadyClock::time_point start_time;
};
#ifdef ARCHITECTURE_x86_64
@@ -93,4 +81,9 @@ std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
#endif
+std::unique_ptr<WallClock> CreateStandardWallClock(u64 emulated_cpu_frequency,
+ u64 emulated_clock_frequency) {
+ return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
+}
+
} // namespace Common
diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h
index 828a523a8..157ec5eae 100644
--- a/src/common/wall_clock.h
+++ b/src/common/wall_clock.h
@@ -55,4 +55,7 @@ private:
[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
u64 emulated_clock_frequency);
+[[nodiscard]] std::unique_ptr<WallClock> CreateStandardWallClock(u64 emulated_cpu_frequency,
+ u64 emulated_clock_frequency);
+
} // namespace Common
diff --git a/src/common/windows/timer_resolution.cpp b/src/common/windows/timer_resolution.cpp
new file mode 100644
index 000000000..29c6e5c7e
--- /dev/null
+++ b/src/common/windows/timer_resolution.cpp
@@ -0,0 +1,109 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <windows.h>
+
+#include "common/windows/timer_resolution.h"
+
+extern "C" {
+// http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FTime%2FNtQueryTimerResolution.html
+NTSYSAPI LONG NTAPI NtQueryTimerResolution(PULONG MinimumResolution, PULONG MaximumResolution,
+ PULONG CurrentResolution);
+
+// http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FTime%2FNtSetTimerResolution.html
+NTSYSAPI LONG NTAPI NtSetTimerResolution(ULONG DesiredResolution, BOOLEAN SetResolution,
+ PULONG CurrentResolution);
+
+// http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FThread%2FNtDelayExecution.html
+NTSYSAPI LONG NTAPI NtDelayExecution(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);
+}
+
+// Defines for compatibility with older Windows 10 SDKs.
+
+#ifndef PROCESS_POWER_THROTTLING_EXECUTION_SPEED
+#define PROCESS_POWER_THROTTLING_EXECUTION_SPEED 0x1
+#endif
+#ifndef PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION
+#define PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION 0x4
+#endif
+
+namespace Common::Windows {
+
+namespace {
+
+using namespace std::chrono;
+
+constexpr nanoseconds ToNS(ULONG hundred_ns) {
+ return nanoseconds{hundred_ns * 100};
+}
+
+constexpr ULONG ToHundredNS(nanoseconds ns) {
+ return static_cast<ULONG>(ns.count()) / 100;
+}
+
+struct TimerResolution {
+ std::chrono::nanoseconds minimum;
+ std::chrono::nanoseconds maximum;
+ std::chrono::nanoseconds current;
+};
+
+TimerResolution GetTimerResolution() {
+ ULONG MinimumTimerResolution;
+ ULONG MaximumTimerResolution;
+ ULONG CurrentTimerResolution;
+ NtQueryTimerResolution(&MinimumTimerResolution, &MaximumTimerResolution,
+ &CurrentTimerResolution);
+ return {
+ .minimum{ToNS(MinimumTimerResolution)},
+ .maximum{ToNS(MaximumTimerResolution)},
+ .current{ToNS(CurrentTimerResolution)},
+ };
+}
+
+void SetHighQoS() {
+ // https://learn.microsoft.com/en-us/windows/win32/procthread/quality-of-service
+ PROCESS_POWER_THROTTLING_STATE PowerThrottling{
+ .Version{PROCESS_POWER_THROTTLING_CURRENT_VERSION},
+ .ControlMask{PROCESS_POWER_THROTTLING_EXECUTION_SPEED |
+ PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION},
+ .StateMask{},
+ };
+ SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &PowerThrottling,
+ sizeof(PROCESS_POWER_THROTTLING_STATE));
+}
+
+} // Anonymous namespace
+
+nanoseconds GetMinimumTimerResolution() {
+ return GetTimerResolution().minimum;
+}
+
+nanoseconds GetMaximumTimerResolution() {
+ return GetTimerResolution().maximum;
+}
+
+nanoseconds GetCurrentTimerResolution() {
+ return GetTimerResolution().current;
+}
+
+nanoseconds SetCurrentTimerResolution(nanoseconds timer_resolution) {
+ // Set the timer resolution, and return the current timer resolution.
+ const auto DesiredTimerResolution = ToHundredNS(timer_resolution);
+ ULONG CurrentTimerResolution;
+ NtSetTimerResolution(DesiredTimerResolution, TRUE, &CurrentTimerResolution);
+ return ToNS(CurrentTimerResolution);
+}
+
+nanoseconds SetCurrentTimerResolutionToMaximum() {
+ SetHighQoS();
+ return SetCurrentTimerResolution(GetMaximumTimerResolution());
+}
+
+void SleepForOneTick() {
+ LARGE_INTEGER DelayInterval{
+ .QuadPart{-1},
+ };
+ NtDelayExecution(FALSE, &DelayInterval);
+}
+
+} // namespace Common::Windows
diff --git a/src/common/windows/timer_resolution.h b/src/common/windows/timer_resolution.h
new file mode 100644
index 000000000..e1e50a62d
--- /dev/null
+++ b/src/common/windows/timer_resolution.h
@@ -0,0 +1,38 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <chrono>
+
+namespace Common::Windows {
+
+/// Returns the minimum (least precise) supported timer resolution in nanoseconds.
+std::chrono::nanoseconds GetMinimumTimerResolution();
+
+/// Returns the maximum (most precise) supported timer resolution in nanoseconds.
+std::chrono::nanoseconds GetMaximumTimerResolution();
+
+/// Returns the current timer resolution in nanoseconds.
+std::chrono::nanoseconds GetCurrentTimerResolution();
+
+/**
+ * Sets the current timer resolution.
+ *
+ * @param timer_resolution Timer resolution in nanoseconds.
+ *
+ * @returns The current timer resolution.
+ */
+std::chrono::nanoseconds SetCurrentTimerResolution(std::chrono::nanoseconds timer_resolution);
+
+/**
+ * Sets the current timer resolution to the maximum supported timer resolution.
+ *
+ * @returns The current timer resolution.
+ */
+std::chrono::nanoseconds SetCurrentTimerResolutionToMaximum();
+
+/// Sleep for one tick of the current timer resolution.
+void SleepForOneTick();
+
+} // namespace Common::Windows
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp
index 8b08332ab..bc1a973b0 100644
--- a/src/common/x64/native_clock.cpp
+++ b/src/common/x64/native_clock.cpp
@@ -6,6 +6,7 @@
#include <thread>
#include "common/atomic_ops.h"
+#include "common/steady_clock.h"
#include "common/uint128.h"
#include "common/x64/native_clock.h"
@@ -39,6 +40,12 @@ static u64 FencedRDTSC() {
}
#endif
+template <u64 Nearest>
+static u64 RoundToNearest(u64 value) {
+ const auto mod = value % Nearest;
+ return mod >= (Nearest / 2) ? (value - mod + Nearest) : (value - mod);
+}
+
u64 EstimateRDTSCFrequency() {
// Discard the first result measuring the rdtsc.
FencedRDTSC();
@@ -46,18 +53,18 @@ u64 EstimateRDTSCFrequency() {
FencedRDTSC();
// Get the current time.
- const auto start_time = std::chrono::steady_clock::now();
+ const auto start_time = Common::SteadyClock::Now();
const u64 tsc_start = FencedRDTSC();
- // Wait for 200 milliseconds.
- std::this_thread::sleep_for(std::chrono::milliseconds{200});
- const auto end_time = std::chrono::steady_clock::now();
+ // Wait for 250 milliseconds.
+ std::this_thread::sleep_for(std::chrono::milliseconds{250});
+ const auto end_time = Common::SteadyClock::Now();
const u64 tsc_end = FencedRDTSC();
// Calculate differences.
const u64 timer_diff = static_cast<u64>(
std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count());
const u64 tsc_diff = tsc_end - tsc_start;
const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff);
- return tsc_freq;
+ return RoundToNearest<1000>(tsc_freq);
}
namespace X64 {