From 1875b8a99ba2b1ad1969dc72fb83f6e49e7a0ea5 Mon Sep 17 00:00:00 2001 From: FakeMichau <49685661+FakeMichau@users.noreply.github.com> Date: Fri, 25 Sep 2026 20:26:03 +0200 Subject: [PATCH] Put GetTimestamp into Utils --- OptiScaler/Util.cpp | 10 +++++++++ OptiScaler/Util.h | 1 + OptiScaler/low_latency/input/input_common.cpp | 16 +++++++------- OptiScaler/low_latency/ll_util.h | 18 ++++----------- .../low_latency_tech/ll_antilag2.cpp | 8 +++---- .../low_latency_tech/ll_latencyflex.cpp | 4 ++-- OptiScaler/misc/FrameLimit.cpp | 22 +++++-------------- OptiScaler/misc/FrameLimit.h | 1 - .../nvapi/fakenvapi/low_latency_d3d.cpp | 16 +++++++------- OptiScaler/nvapi/fakenvapi/low_latency_vk.cpp | 16 +++++++------- 10 files changed, 51 insertions(+), 61 deletions(-) diff --git a/OptiScaler/Util.cpp b/OptiScaler/Util.cpp index 1e68b2f1..f7ac6fa7 100644 --- a/OptiScaler/Util.cpp +++ b/OptiScaler/Util.cpp @@ -933,3 +933,13 @@ std::map Util::GetDriverStore() return result; } + +uint64_t Util::GetTimestamp() +{ + FILETIME fileTime; + GetSystemTimePreciseAsFileTime(&fileTime); + + uint64_t time = (static_cast(fileTime.dwHighDateTime) << 32) | fileTime.dwLowDateTime; + + return time * 100; +} diff --git a/OptiScaler/Util.h b/OptiScaler/Util.h index 77e73f3e..f3975eef 100644 --- a/OptiScaler/Util.h +++ b/OptiScaler/Util.h @@ -57,6 +57,7 @@ void LoadProxyLibrary(const std::wstring& name, const std::wstring& optiPath, co HMODULE* memoryModule, HMODULE* loadedModule); std::map GetDriverStore(); +uint64_t GetTimestamp(); template void DelayedDestroy(std::unique_ptr ptr) { diff --git a/OptiScaler/low_latency/input/input_common.cpp b/OptiScaler/low_latency/input/input_common.cpp index 33a9e55e..678027d0 100644 --- a/OptiScaler/low_latency/input/input_common.cpp +++ b/OptiScaler/low_latency/input/input_common.cpp @@ -250,7 +250,7 @@ bool InputCommon::update_low_latency_tech(IUnknown* pDevice, std::optionalsimStartTime = last_sim_start; break; case MarkerType::SIMULATION_END: - current_report->simEndTime = get_timestamp() / 1000; + current_report->simEndTime = Util::GetTimestamp() / 1000; break; case MarkerType::RENDERSUBMIT_START: - current_report->renderSubmitStartTime = get_timestamp() / 1000; + current_report->renderSubmitStartTime = Util::GetTimestamp() / 1000; break; case MarkerType::RENDERSUBMIT_END: - current_report->renderSubmitEndTime = get_timestamp() / 1000; + current_report->renderSubmitEndTime = Util::GetTimestamp() / 1000; break; case MarkerType::PRESENT_START: - current_report->presentStartTime = get_timestamp() / 1000; + current_report->presentStartTime = Util::GetTimestamp() / 1000; break; case MarkerType::PRESENT_END: - current_report->presentEndTime = get_timestamp() / 1000; + current_report->presentEndTime = Util::GetTimestamp() / 1000; break; case MarkerType::INPUT_SAMPLE: - current_report->inputSampleTime = get_timestamp() / 1000; + current_report->inputSampleTime = Util::GetTimestamp() / 1000; break; default: break; diff --git a/OptiScaler/low_latency/ll_util.h b/OptiScaler/low_latency/ll_util.h index ef8d90a0..47c065c6 100644 --- a/OptiScaler/low_latency/ll_util.h +++ b/OptiScaler/low_latency/ll_util.h @@ -56,16 +56,6 @@ void tonvss(NvAPI_ShortString nvss, std::string str); if (std::string(it->func) == #method) \ return fakenvapi::idToFuncMapping.insert({ id, (void*) nvapi_calls::method }).first->second; -static inline uint64_t get_timestamp() -{ - FILETIME fileTime; - GetSystemTimePreciseAsFileTime(&fileTime); - - uint64_t time = (static_cast(fileTime.dwHighDateTime) << 32) | fileTime.dwLowDateTime; - - return time * 100; -} - // https://learn.microsoft.com/en-us/windows/win32/sync/using-waitable-timer-objects inline int timer_sleep(int64_t hundred_ns) { @@ -88,11 +78,11 @@ inline int timer_sleep(int64_t hundred_ns) inline int busywait_sleep(int64_t ns) { - auto current_time = get_timestamp(); + auto current_time = Util::GetTimestamp(); auto wait_until = current_time + ns; while (current_time < wait_until) { - current_time = get_timestamp(); + current_time = Util::GetTimestamp(); } return 0; } @@ -103,13 +93,13 @@ inline int eepy(int64_t ns) int status; - auto current_time = get_timestamp(); + auto current_time = Util::GetTimestamp(); if (ns <= busywait_threshold) status = busywait_sleep(ns); else status = timer_sleep((ns - busywait_threshold) / 100); - if (int64_t sleep_deviation = ns - (get_timestamp() - current_time); sleep_deviation > 0 && !status) + if (int64_t sleep_deviation = ns - (Util::GetTimestamp() - current_time); sleep_deviation > 0 && !status) status = busywait_sleep(sleep_deviation); return status; diff --git a/OptiScaler/low_latency/low_latency_tech/ll_antilag2.cpp b/OptiScaler/low_latency/low_latency_tech/ll_antilag2.cpp index 3f531a2b..8302a651 100644 --- a/OptiScaler/low_latency/low_latency_tech/ll_antilag2.cpp +++ b/OptiScaler/low_latency/low_latency_tech/ll_antilag2.cpp @@ -11,13 +11,13 @@ inline HRESULT AntiLag2::al2_sleep() // if (effective_fg_state && minimum_interval_us != 0) { // static uint64_t previous_frame_time = 0; - // uint64_t current_time = get_timestamp(); + // uint64_t current_time = Util::GetTimestamp(); // uint64_t frame_time = current_time - previous_frame_time; // if (frame_time < 1000 * minimum_interval_us) { // if (auto res = eepy(minimum_interval_us * 1000 - frame_time); res) // LOG_ERROR("Sleep command failed: {}", res); // } - // previous_frame_time = get_timestamp(); + // previous_frame_time = Util::GetTimestamp(); // } else { // max_fps = minimum_interval_us > 0 ? (int) std::round(1000000.0f / minimum_interval_us) : 0; // } @@ -25,14 +25,14 @@ inline HRESULT AntiLag2::al2_sleep() HRESULT result = {}; - // auto pre_sleep = get_timestamp(); + // auto pre_sleep = Util::GetTimestamp(); if (dx12_ctx.m_pAntiLagAPI) result = AMD::AntiLag2DX12::Update(&dx12_ctx, is_enabled(), max_fps); else if (dx11_ctx.m_pAntiLagAPI) result = AMD::AntiLag2DX11::Update(&dx11_ctx, is_enabled(), max_fps); - // log_event("al2_sleep", "{}", get_timestamp() - pre_sleep); + // log_event("al2_sleep", "{}", Util::GetTimestamp() - pre_sleep); LOG_TRACE_LOWLATENCY("FSR Anti-Lag 2.0 Call Spot: {}", current_call_spot == CallSpot::SimulationStart ? "SimulationStart" : "SleepCall"); diff --git a/OptiScaler/low_latency/low_latency_tech/ll_latencyflex.cpp b/OptiScaler/low_latency/low_latency_tech/ll_latencyflex.cpp index 0997f12d..0ba8975b 100644 --- a/OptiScaler/low_latency/low_latency_tech/ll_latencyflex.cpp +++ b/OptiScaler/low_latency/low_latency_tech/ll_latencyflex.cpp @@ -29,7 +29,7 @@ void LatencyFlex::lfx_sleep(uint64_t reflex_frame_id) ctx->Reset(); } - uint64_t current_timestamp = get_timestamp(); + uint64_t current_timestamp = Util::GetTimestamp(); uint64_t timestamp; // Set FPS Limiter @@ -85,7 +85,7 @@ void LatencyFlex::lfx_sleep(uint64_t reflex_frame_id) void LatencyFlex::lfx_end_frame(uint64_t reflex_frame_id) { - auto current_timestamp = get_timestamp(); + auto current_timestamp = Util::GetTimestamp(); mutex.lock(); auto frame_id = (LFXMode) Config::Instance()->FN_LatencyFlexMode.value_or_default() == LFXMode::ReflexIDs ? reflex_frame_id diff --git a/OptiScaler/misc/FrameLimit.cpp b/OptiScaler/misc/FrameLimit.cpp index a66401ee..d623690d 100644 --- a/OptiScaler/misc/FrameLimit.cpp +++ b/OptiScaler/misc/FrameLimit.cpp @@ -4,16 +4,6 @@ #include "Config.h" // #include "hooks/D3D11Hooks.h" -inline uint64_t FrameLimit::get_timestamp() -{ - FILETIME fileTime; - GetSystemTimePreciseAsFileTime(&fileTime); - - uint64_t time = (static_cast(fileTime.dwHighDateTime) << 32) | fileTime.dwLowDateTime; - - return time * 100; -} - // https://learn.microsoft.com/en-us/windows/win32/sync/using-waitable-timer-objects inline int FrameLimit::timer_sleep(int64_t hundred_ns) { @@ -36,11 +26,11 @@ inline int FrameLimit::timer_sleep(int64_t hundred_ns) inline int FrameLimit::busywait_sleep(int64_t ns) { - auto current_time = get_timestamp(); + auto current_time = Util::GetTimestamp(); auto wait_until = current_time + ns; while (current_time < wait_until) { - current_time = get_timestamp(); + current_time = Util::GetTimestamp(); } return 0; } @@ -49,13 +39,13 @@ inline int FrameLimit::combined_sleep(int64_t ns) { constexpr int64_t busywait_threshold = 2'000'000; // 2ms int status {}; - auto current_time = get_timestamp(); + auto current_time = Util::GetTimestamp(); if (ns <= busywait_threshold) status = busywait_sleep(ns); else status = timer_sleep((ns - busywait_threshold) / 100); - if (int64_t sleep_deviation = ns - (get_timestamp() - current_time); sleep_deviation > 0 && !status) + if (int64_t sleep_deviation = ns - (Util::GetTimestamp() - current_time); sleep_deviation > 0 && !status) status = busywait_sleep(sleep_deviation); return status; @@ -71,13 +61,13 @@ void FrameLimit::sleep(bool fgActive) min_interval_us *= 2; static uint64_t previous_frame_time = 0; - uint64_t current_time = get_timestamp(); + uint64_t current_time = Util::GetTimestamp(); uint64_t frame_time = current_time - previous_frame_time; if (frame_time < 1000 * min_interval_us) { if (auto res = combined_sleep(min_interval_us * 1000 - frame_time); res) LOG_ERROR("Sleep command failed: {}", res); } - previous_frame_time = get_timestamp(); + previous_frame_time = Util::GetTimestamp(); } } diff --git a/OptiScaler/misc/FrameLimit.h b/OptiScaler/misc/FrameLimit.h index 0ad76015..34b06704 100644 --- a/OptiScaler/misc/FrameLimit.h +++ b/OptiScaler/misc/FrameLimit.h @@ -3,7 +3,6 @@ class FrameLimit { - static uint64_t get_timestamp(); static int timer_sleep(int64_t hundred_ns); static int busywait_sleep(int64_t ns); static int combined_sleep(int64_t ns); diff --git a/OptiScaler/nvapi/fakenvapi/low_latency_d3d.cpp b/OptiScaler/nvapi/fakenvapi/low_latency_d3d.cpp index 21d050a7..371d8f28 100644 --- a/OptiScaler/nvapi/fakenvapi/low_latency_d3d.cpp +++ b/OptiScaler/nvapi/fakenvapi/low_latency_d3d.cpp @@ -174,7 +174,7 @@ void LowLatency::get_latency_result(NV_LATENCY_RESULT_PARAMS* pGetLatencyParams) void LowLatency::add_marker_to_report(NV_LATENCY_MARKER_PARAMS* pSetLatencyMarkerParams) { - auto current_timestamp = get_timestamp() / 1000; + auto current_timestamp = Util::GetTimestamp() / 1000; static auto last_sim_start = current_timestamp; static auto _2nd_last_sim_start = current_timestamp; auto current_report = &frame_reports[pSetLatencyMarkerParams->frameID % FRAME_REPORTS_BUFFER_SIZE]; @@ -197,26 +197,26 @@ void LowLatency::add_marker_to_report(NV_LATENCY_MARKER_PARAMS* pSetLatencyMarke { case SIMULATION_START: _2nd_last_sim_start = last_sim_start; - last_sim_start = get_timestamp() / 1000; + last_sim_start = Util::GetTimestamp() / 1000; current_report->simStartTime = last_sim_start; break; case SIMULATION_END: - current_report->simEndTime = get_timestamp() / 1000; + current_report->simEndTime = Util::GetTimestamp() / 1000; break; case RENDERSUBMIT_START: - current_report->renderSubmitStartTime = get_timestamp() / 1000; + current_report->renderSubmitStartTime = Util::GetTimestamp() / 1000; break; case RENDERSUBMIT_END: - current_report->renderSubmitEndTime = get_timestamp() / 1000; + current_report->renderSubmitEndTime = Util::GetTimestamp() / 1000; break; case PRESENT_START: - current_report->presentStartTime = get_timestamp() / 1000; + current_report->presentStartTime = Util::GetTimestamp() / 1000; break; case PRESENT_END: - current_report->presentEndTime = get_timestamp() / 1000; + current_report->presentEndTime = Util::GetTimestamp() / 1000; break; case INPUT_SAMPLE: - current_report->inputSampleTime = get_timestamp() / 1000; + current_report->inputSampleTime = Util::GetTimestamp() / 1000; break; default: break; diff --git a/OptiScaler/nvapi/fakenvapi/low_latency_vk.cpp b/OptiScaler/nvapi/fakenvapi/low_latency_vk.cpp index 51285168..9fb85548 100644 --- a/OptiScaler/nvapi/fakenvapi/low_latency_vk.cpp +++ b/OptiScaler/nvapi/fakenvapi/low_latency_vk.cpp @@ -108,7 +108,7 @@ void LowLatency::get_latency_result(NV_VULKAN_LATENCY_RESULT_PARAMS* pGetLatency void LowLatency::add_marker_to_report(NV_VULKAN_LATENCY_MARKER_PARAMS* pSetLatencyMarkerParams) { - auto current_timestamp = get_timestamp() / 1000; + auto current_timestamp = Util::GetTimestamp() / 1000; static auto last_sim_start = current_timestamp; static auto _2nd_last_sim_start = current_timestamp; auto current_report = &frame_reports[pSetLatencyMarkerParams->frameID % FRAME_REPORTS_BUFFER_SIZE]; @@ -131,26 +131,26 @@ void LowLatency::add_marker_to_report(NV_VULKAN_LATENCY_MARKER_PARAMS* pSetLaten { case VULKAN_SIMULATION_START: _2nd_last_sim_start = last_sim_start; - last_sim_start = get_timestamp() / 1000; + last_sim_start = Util::GetTimestamp() / 1000; current_report->simStartTime = last_sim_start; break; case VULKAN_SIMULATION_END: - current_report->simEndTime = get_timestamp() / 1000; + current_report->simEndTime = Util::GetTimestamp() / 1000; break; case VULKAN_RENDERSUBMIT_START: - current_report->renderSubmitStartTime = get_timestamp() / 1000; + current_report->renderSubmitStartTime = Util::GetTimestamp() / 1000; break; case VULKAN_RENDERSUBMIT_END: - current_report->renderSubmitEndTime = get_timestamp() / 1000; + current_report->renderSubmitEndTime = Util::GetTimestamp() / 1000; break; case VULKAN_PRESENT_START: - current_report->presentStartTime = get_timestamp() / 1000; + current_report->presentStartTime = Util::GetTimestamp() / 1000; break; case VULKAN_PRESENT_END: - current_report->presentEndTime = get_timestamp() / 1000; + current_report->presentEndTime = Util::GetTimestamp() / 1000; break; case VULKAN_INPUT_SAMPLE: - current_report->inputSampleTime = get_timestamp() / 1000; + current_report->inputSampleTime = Util::GetTimestamp() / 1000; break; default: break;