From f312c408f0c176542546619e64c3df09f15cae89 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 20 Dec 2023 22:21:31 +0100 Subject: [PATCH] NVIDIA: check if adapter is from NVIDIA in GFX_SetLatencyMarker() Moved adapter vendor check to its own func, added a global which could be used to fully disable the low latency system. Also added groundwork for the (future) PCL stats implementation. --- src/engine/sys_dll2.cpp | 6 +--- src/geforce/reflex.cpp | 47 ++++++++++++++++++++------ src/geforce/reflex.h | 3 ++ src/materialsystem/cmaterialsystem.cpp | 27 +++++++++++++++ src/materialsystem/cmaterialsystem.h | 8 +++++ 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/src/engine/sys_dll2.cpp b/src/engine/sys_dll2.cpp index 3c8c67ac..46c583ee 100644 --- a/src/engine/sys_dll2.cpp +++ b/src/engine/sys_dll2.cpp @@ -184,11 +184,7 @@ bool CEngineAPI::MainLoop() } #ifndef DEDICATED - const MaterialAdapterInfo_t& adapterInfo = g_pMaterialAdapterMgr->GetAdapterInfo(); - - // Only run on NVIDIA display drivers; AMD and Intel are not - // supported by NVIDIA Reflex. - if (adapterInfo.m_VendorID == NVIDIA_VENDOR_ID) + if (GFX_IsLowLatencySDKEnabled()) { if (GFX_HasPendingLowLatencyParameterUpdates()) { diff --git a/src/geforce/reflex.cpp b/src/geforce/reflex.cpp index a59afacd..1e8bd321 100644 --- a/src/geforce/reflex.cpp +++ b/src/geforce/reflex.cpp @@ -5,6 +5,9 @@ //===========================================================================// #include "reflex.h" #include "mathlib/mathlib.h" +#include "materialsystem/cmaterialsystem.h" + +static bool s_LowLatencySDKEnabled = false; // If false, the system will call 'NvAPI_D3D_SetSleepMode' to update the parameters. bool s_ReflexModeInfoUpToDate = false; @@ -17,6 +20,29 @@ NvAPI_Status s_ReflexModeUpdateStatus = NvAPI_Status::NVAPI_OK; NvU64 s_ReflexFrameNumber = 0; NvU64 s_ReflexLastFrameNumber = 0; +//----------------------------------------------------------------------------- +// Purpose: enable/disable low latency SDK +// Input : enable - +//----------------------------------------------------------------------------- +void GFX_EnableLowLatencySDK(const bool enable) +{ + s_LowLatencySDKEnabled = enable; +} + +//----------------------------------------------------------------------------- +// Purpose: whether we should run the low latency SDK +//----------------------------------------------------------------------------- +bool GFX_IsLowLatencySDKEnabled(void) +{ + if (!s_LowLatencySDKEnabled) + return false; + + const MaterialAdapterInfo_t& adapterInfo = g_pMaterialAdapterMgr->GetAdapterInfo(); + // Only run on NVIDIA display drivers; AMD and Intel are not + // supported by NVIDIA Reflex. + return adapterInfo.m_VendorID == NVIDIA_VENDOR_ID; +} + //----------------------------------------------------------------------------- // Purpose: mark the parameters as out-of-date; force update next frame //----------------------------------------------------------------------------- @@ -101,6 +127,7 @@ void GFX_UpdateLowLatencyParameters(IUnknown* device, const bool useLowLatencyMo //----------------------------------------------------------------------------- void GFX_RunLowLatencyFrame(IUnknown* device) { + Assert(device); const NvU64 currentFrameNumber = GFX_GetFrameNumber(); if (s_ReflexLastFrameNumber == currentFrameNumber) @@ -121,15 +148,15 @@ void GFX_RunLowLatencyFrame(IUnknown* device) void GFX_SetLatencyMarker(IUnknown* device, const NV_LATENCY_MARKER_TYPE markerType) { - // TODO[ AMOS ]: should we keep calling this, even when the call to - // 'NvAPI_D3D_SetSleepMode(...)' has failed? - if (GFX_ParameterUpdateWasSuccessful()) - { - NV_LATENCY_MARKER_PARAMS params = {}; - params.version = NV_LATENCY_MARKER_PARAMS_VER1; - params.frameID = s_ReflexFrameNumber; - params.markerType = markerType; + Assert(device); - NvAPI_D3D_SetLatencyMarker(device, ¶ms); - } + if (!GFX_IsLowLatencySDKEnabled()) + return; + + NV_LATENCY_MARKER_PARAMS params = {}; + params.version = NV_LATENCY_MARKER_PARAMS_VER1; + params.frameID = s_ReflexFrameNumber; + params.markerType = markerType; + + NvAPI_D3D_SetLatencyMarker(device, ¶ms); } diff --git a/src/geforce/reflex.h b/src/geforce/reflex.h index 7bfe745e..3244629f 100644 --- a/src/geforce/reflex.h +++ b/src/geforce/reflex.h @@ -1,6 +1,9 @@ #ifndef GFSDK_REFLEX_H #define GFSDK_REFLEX_H +void GFX_EnableLowLatencySDK(const bool enable); +bool GFX_IsLowLatencySDKEnabled(void); + void GFX_MarkLowLatencyParametersOutOfDate(void); bool GFX_HasPendingLowLatencyParameterUpdates(void); diff --git a/src/materialsystem/cmaterialsystem.cpp b/src/materialsystem/cmaterialsystem.cpp index 9e36abd7..671dae11 100644 --- a/src/materialsystem/cmaterialsystem.cpp +++ b/src/materialsystem/cmaterialsystem.cpp @@ -5,15 +5,21 @@ //===========================================================================// #include "core/stdafx.h" #include "tier0/crashhandler.h" +#include "tier0/commandline.h" #include "tier1/cvar.h" #include "vpc/keyvalues.h" #include "rtech/rtech_utils.h" #include "engine/cmodel_bsp.h" +#include "geforce/reflex.h" #ifndef MATERIALSYSTEM_NODX #include "materialsystem/cmaterialglue.h" #endif // !MATERIALSYSTEM_NODX #include "materialsystem/cmaterialsystem.h" +#ifndef MATERIALSYSTEM_NODX +//PCLSTATS_DEFINE() +#endif // MATERIALSYSTEM_NODX + //----------------------------------------------------------------------------- // Purpose: initialization of the material system //----------------------------------------------------------------------------- @@ -30,10 +36,30 @@ InitReturnVal_t CMaterialSystem::Init(CMaterialSystem* thisptr) return INIT_FAILED; #else // Initialize as usual. + GFX_EnableLowLatencySDK(!CommandLine()->CheckParm("-gfx_nvnDisableLowLatency")); + + //if (GFX_IsLowLatencySDKEnabled()) + //{ + // PCLSTATS_INIT(0); + //} + return CMaterialSystem__Init(thisptr); #endif } +//----------------------------------------------------------------------------- +// Purpose: shutdown of the material system +//----------------------------------------------------------------------------- +int CMaterialSystem::Shutdown(CMaterialSystem* thisptr) +{ + //if (GFX_IsLowLatencySDKEnabled()) + //{ + // PCLSTATS_SHUTDOWN(); + //} + + return CMaterialSystem__Shutdown(thisptr); +} + #ifndef MATERIALSYSTEM_NODX //--------------------------------------------------------------------------------- // Purpose: loads and processes STBSP files @@ -124,6 +150,7 @@ Vector2D CMaterialSystem::GetScreenSize(CMaterialSystem* pMatSys) void VMaterialSystem::Detour(const bool bAttach) const { DetourSetup(&CMaterialSystem__Init, &CMaterialSystem::Init, bAttach); + DetourSetup(&CMaterialSystem__Shutdown, &CMaterialSystem::Shutdown, bAttach); #ifndef MATERIALSYSTEM_NODX DetourSetup(&v_StreamDB_Init, &StreamDB_Init, bAttach); DetourSetup(&v_DispatchDrawCall, &DispatchDrawCall, bAttach); diff --git a/src/materialsystem/cmaterialsystem.h b/src/materialsystem/cmaterialsystem.h index 7a3bc470..f2422631 100644 --- a/src/materialsystem/cmaterialsystem.h +++ b/src/materialsystem/cmaterialsystem.h @@ -9,6 +9,7 @@ class CMaterialSystem { public: static InitReturnVal_t Init(CMaterialSystem* thisptr); + static int Shutdown(CMaterialSystem* thisptr); #ifndef MATERIALSYSTEM_NODX static CMaterialGlue* FindMaterialEx(CMaterialSystem* pMatSys, const char* pMaterialName, uint8_t nMaterialType, int nUnk, bool bComplain); static Vector2D GetScreenSize(CMaterialSystem* pMatSys = nullptr); @@ -52,6 +53,9 @@ inline CMaterialDeviceMgr* g_pMaterialAdapterMgr = nullptr; inline CMemory p_CMaterialSystem__Init; inline InitReturnVal_t(*CMaterialSystem__Init)(CMaterialSystem* thisptr); +inline CMemory p_CMaterialSystem__Shutdown; +inline int(*CMaterialSystem__Shutdown)(CMaterialSystem* thisptr); + inline CMemory p_CMaterialSystem__Disconnect; inline void(*CMaterialSystem__Disconnect)(void); @@ -91,6 +95,7 @@ class VMaterialSystem : public IDetour { LogConAdr("CMaterial::`vftable'", reinterpret_cast(g_pMaterialVFTable)); LogFunAdr("CMaterialSystem::Init", p_CMaterialSystem__Init.GetPtr()); + LogFunAdr("CMaterialSystem::Shutdown", p_CMaterialSystem__Shutdown.GetPtr()); LogFunAdr("CMaterialSystem::Disconnect", p_CMaterialSystem__Disconnect.GetPtr()); #ifndef MATERIALSYSTEM_NODX LogFunAdr("CMaterialSystem::FindMaterialEx", p_CMaterialSystem__FindMaterialEx.GetPtr()); @@ -111,6 +116,9 @@ class VMaterialSystem : public IDetour p_CMaterialSystem__Init = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??"); CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/ + p_CMaterialSystem__Shutdown = g_GameDll.FindPatternSIMD("48 83 EC 58 48 89 6C 24 ??"); + CMaterialSystem__Shutdown = p_CMaterialSystem__Shutdown.RCast(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/ + p_CMaterialSystem__Disconnect = g_GameDll.FindPatternSIMD("48 83 EC 28 8B 0D ?? ?? ?? ?? 48 89 6C 24 ??"); CMaterialSystem__Disconnect = p_CMaterialSystem__Disconnect.RCast(); #ifndef MATERIALSYSTEM_NODX