From 22a1d4a3b7aebbd296afb04d6d2cbc5358ad54d5 Mon Sep 17 00:00:00 2001 From: FakeMichau <49685661+FakeMichau@users.noreply.github.com> Date: Sat, 14 Sep 2024 17:46:53 +0200 Subject: [PATCH] Hack against RTSS --- OptiScaler/nvapi/fakenvapi/fakenvapi.cpp | 16 ++++++++++++++++ OptiScaler/nvapi/fakenvapi/lowlatency.h | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/OptiScaler/nvapi/fakenvapi/fakenvapi.cpp b/OptiScaler/nvapi/fakenvapi/fakenvapi.cpp index cb22f3b9..c293c6d7 100644 --- a/OptiScaler/nvapi/fakenvapi/fakenvapi.cpp +++ b/OptiScaler/nvapi/fakenvapi/fakenvapi.cpp @@ -335,6 +335,10 @@ namespace nvd { NvAPI_Status __cdecl NvAPI_D3D_SetLatencyMarker(IUnknown* pDev, NV_LATENCY_MARKER_PARAMS* pSetLatencyMarkerParams) { if (!pDev) return Error(); + + if (lowlatency_ctx.ignore_frameid(pSetLatencyMarkerParams->frameID)) + return NVAPI_OK; + spdlog::debug("markerType: {}, frame id: {}", (unsigned int)pSetLatencyMarkerParams->markerType, (unsigned long long)pSetLatencyMarkerParams->frameID); lowlatency_ctx.init_al2(pDev); switch (pSetLatencyMarkerParams->markerType) { @@ -357,6 +361,15 @@ namespace nvd { NvAPI_Status __cdecl NvAPI_D3D_Sleep(IUnknown* pDevice) { if (!pDevice) return Error(); + + // HACK for RTSS injecting markers and sleep even when a game sends them already + // TODO: option to reset the accepted_thread_id? + static std::thread::id accepted_thread_id = std::this_thread::get_id(); + std::thread::id current_thread_id = std::this_thread::get_id(); + spdlog::trace("Sleep called"); + if (accepted_thread_id != current_thread_id && lowlatency_ctx.is_double_markers()) + return NVAPI_OK; // skip that thread + lowlatency_ctx.init_al2(pDevice); lowlatency_ctx.call_spot = SleepCall; spdlog::debug("LowLatency update called on sleep with result: {}", lowlatency_ctx.update()); @@ -535,6 +548,9 @@ namespace nvd { } NvAPI_Status __cdecl NvAPI_D3D12_SetAsyncFrameMarker(ID3D12CommandQueue* pCommandQueue, NV_ASYNC_FRAME_MARKER_PARAMS* pSetAsyncFrameMarkerParams) { + if (lowlatency_ctx.ignore_frameid(pSetAsyncFrameMarkerParams->frameID)) + return NVAPI_OK; + if (pSetAsyncFrameMarkerParams->markerType == OUT_OF_BAND_PRESENT_START) { constexpr unsigned int history_size = 10; static NvU64 counter = 0; diff --git a/OptiScaler/nvapi/fakenvapi/lowlatency.h b/OptiScaler/nvapi/fakenvapi/lowlatency.h index f70123a4..dc3ab45a 100644 --- a/OptiScaler/nvapi/fakenvapi/lowlatency.h +++ b/OptiScaler/nvapi/fakenvapi/lowlatency.h @@ -55,6 +55,7 @@ class LowLatency { unsigned long min_interval_us = 0; bool al_available = false; bool force_latencyflex = false; + bool double_markers = false; ForceReflex force_reflex = InGame; static inline uint64_t GetTimestamp() { @@ -194,4 +195,22 @@ public: Mode get_mode() { return mode; } + + bool is_double_markers() { + return double_markers; + } + + bool ignore_frameid(uint64_t frameid) { + constexpr uint64_t allowed_frameid_gap = 64; + static uint64_t max_frameid = allowed_frameid_gap; + if (bool result = frameid > max_frameid + allowed_frameid_gap; result) { + if (!double_markers) + spdlog::warn("Double reflex latency markers detected, disable RTSS!"); + double_markers = true; + return result; + } else { + if (frameid > max_frameid) max_frameid = frameid; + return result; + } + } }; \ No newline at end of file