Initial implementation of NVIDIA Reflex Low Latency timing

Propagated latency markers in an attempt to further reduce input latency. Code is pending rigorous testing on several systems, including systems powered by an AMD GPU to make sure we are not impacting performance on systems that does NOT support NVIDIA Reflex.
This commit is contained in:
Kawe Mazidjatari 2023-09-12 11:06:16 +02:00
parent 9df90d4a25
commit ea6c15df4c
9 changed files with 111 additions and 9 deletions

View File

@ -317,7 +317,7 @@ void ConVar_StaticInit(void)
fps_max_gfx = ConVar::StaticCreate("fps_max_gfx", "0", FCVAR_RELEASE, "Frame rate limiter using NVIDIA Reflex Low Latency SDK. -1 indicates use the desktop refresh. 0 is disabled.", true, -1.f, false, 0.f, nullptr, nullptr);
gfx_nvnUseLowLatency = ConVar::StaticCreate("gfx_nvnUseLowLatency" , "1", FCVAR_RELEASE | FCVAR_ARCHIVE, "Enables NVIDIA Reflex Low Latency SDK." , false, 0.f, false, 0.f, nullptr, nullptr);
gfx_nvnUseLowLatencyBoost = ConVar::StaticCreate("gfx_nvnUseLowLatencyBoost", "1", FCVAR_RELEASE | FCVAR_ARCHIVE, "Enables NVIDIA Reflex Low Latency Boost.", false, 0.f, false, 0.f, nullptr, nullptr);
gfx_nvnUseMarkersToOptimize = ConVar::StaticCreate("gfx_nvnUseMarkersToOptimize", "0", FCVAR_DEVELOPMENTONLY /*!!! MAKE RELEASE ONCE IMPLEMENTED !!!*/ | FCVAR_ARCHIVE, "Enables NVIDIA Reflex Low Latency Timing.", false, 0.f, false, 0.f, nullptr, nullptr);
gfx_nvnUseMarkersToOptimize = ConVar::StaticCreate("gfx_nvnUseMarkersToOptimize", "1", FCVAR_RELEASE | FCVAR_ARCHIVE, "Enables NVIDIA Reflex Low Latency Timing.", false, 0.f, false, 0.f, nullptr, nullptr);
#endif // !DEDICATED
//-------------------------------------------------------------------------

View File

@ -11,6 +11,8 @@
#include "engine/client/cl_rcon.h"
#include "networksystem/bansystem.h"
#include "vpc/keyvalues.h"
#include "windows/id3dx.h"
#include "geforce/reflex.h"
#include "vengineclient_impl.h"
#include "cdll_engine_int.h"
/*****************************************************************************/
@ -21,7 +23,15 @@
//-----------------------------------------------------------------------------
void CHLClient::FrameStageNotify(CHLClient* pHLClient, ClientFrameStage_t frameStage)
{
// Must be performed before the call, before scene starts rendering.
if (frameStage == ClientFrameStage_t::FRAME_RENDER_START)
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_START);
CHLClient_FrameStageNotify(pHLClient, frameStage);
// Must be performed after the call, after the scene has been rendered.
if (frameStage == ClientFrameStage_t::FRAME_RENDER_END)
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_END);
}
//-----------------------------------------------------------------------------

View File

@ -10,7 +10,7 @@ enum class ClientFrameStage_t : int
FRAME_UNDEFINED = -1, // (haven't run any frames yet)
FRAME_START,
// A network packet is being recieved
// A network packet is being received
FRAME_NET_UPDATE_START,
// Data has been received and we're going to start calling PostDataUpdate
FRAME_NET_UPDATE_POSTDATAUPDATE_START,

View File

@ -10,14 +10,21 @@
#include "cl_main.h"
#include "engine/net.h"
#include "cdll_engine_int.h"
#include "windows/id3dx.h"
#include "geforce/reflex.h"
static float s_lastMovementCall = 0.0;
static float s_LastFrameTime = 0.0;
// The game supports sending multiple movement frames in a single simulation
// frame, therefore, we need to track when the last call was, and make sure
// we only call the latency marker once per frame.
static int s_LastMovementReflexFrame = -1;
//-----------------------------------------------------------------------------
// Purpose: run client's movement frame
//-----------------------------------------------------------------------------
void H_CL_Move()
void CL_MoveEx()
{
CClientState* cl = GetBaseLocalClient();
@ -139,6 +146,24 @@ void H_CL_Move()
}
}
//-----------------------------------------------------------------------------
// Purpose: hook and run latency markers before simulation
//-----------------------------------------------------------------------------
void H_CL_Move()
{
int currentReflexFrame = GFX_GetFrameNumber();
if (currentReflexFrame != s_LastMovementReflexFrame)
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_START);
CL_MoveEx();
if (currentReflexFrame != s_LastMovementReflexFrame)
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_END);
s_LastMovementReflexFrame = currentReflexFrame;
}
void VCL_Main::Attach() const
{
DetourAttach(&CL_Move, &H_CL_Move);

View File

@ -1,4 +1,4 @@
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//===== Copyright <EFBFBD> 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
@ -155,6 +155,17 @@ void CEngineAPI::VSetStartupInfo(CEngineAPI* pEngineAPI, StartupInfo_t* pStartup
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1)
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEngineAPI::PumpMessages()
{
#ifndef DEDICATED
GFX_SetLatencyMarker(D3D11Device(), PC_LATENCY_PING);
CEngineAPI_PumpMessages();
#endif // !DEDICATED
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
@ -193,9 +204,9 @@ bool CEngineAPI::MainLoop()
fpsMax = 0.0f; // Don't let NVIDIA limit the frame rate.
}
GFX_RunLowLatencySDK(D3D11Device(), bUseLowLatencyMode,
GFX_RunLowLatencyFrame(D3D11Device(), bUseLowLatencyMode,
bUseLowLatencyBoost, bUseLowLatencyTiming, fpsMax);
CEngineAPI_PumpMessages();
CEngineAPI::PumpMessages();
#endif // !DEDICATED
g_pEngine->Frame();

View File

@ -34,6 +34,7 @@ public:
static bool VModInit(CEngineAPI* pEngineAPI, const char* pModName, const char* pGameDir);
static void VSetStartupInfo(CEngineAPI* pEngineAPI, StartupInfo_t* pStartupInfo);
static void PumpMessages();
static bool MainLoop();
//private:
void* m_hEditorHWnd;

View File

@ -6,22 +6,48 @@
#include "reflex.h"
#include "mathlib/mathlib.h"
#define GFX_NVN_MAX_FRAME_COUNT 4000
// Static frame number counter for latency markers.
int s_ReflexFrameNumber = -1;
//-----------------------------------------------------------------------------
// Purpose: runs the low latency sdk
// Purpose: gets the reflex frame number
// Output : int
//-----------------------------------------------------------------------------
int GFX_GetFrameNumber(void)
{
return s_ReflexFrameNumber;
}
//-----------------------------------------------------------------------------
// Purpose: increments the reflex frame number
//-----------------------------------------------------------------------------
void GFX_IncrementFrameNumber(void)
{
// Start over again to make sure this never overflows. Each frame number
// within the last 1000 must be unique, so its safe to reset after 4000.
if (++s_ReflexFrameNumber >= GFX_NVN_MAX_FRAME_COUNT)
s_ReflexFrameNumber = 0;
}
//-----------------------------------------------------------------------------
// Purpose: runs a frame of the low latency sdk
// Input : *device -
// useLowLatencyMode -
// useLowLatencyBoost -
// useMarkersToOptimize -
// maxFramesPerSecond -
//-----------------------------------------------------------------------------
void GFX_RunLowLatencySDK(IUnknown* device, const bool useLowLatencyMode,
void GFX_RunLowLatencyFrame(IUnknown* device, const bool useLowLatencyMode,
const bool useLowLatencyBoost, const bool useMarkersToOptimize,
const float maxFramesPerSecond)
{
Assert(device);
Assert(IsFinite(maxFramesPerSecond));
GFX_IncrementFrameNumber();
NV_SET_SLEEP_MODE_PARAMS params = {};
params.version = NV_SET_SLEEP_MODE_PARAMS_VER1;
@ -37,3 +63,20 @@ void GFX_RunLowLatencySDK(IUnknown* device, const bool useLowLatencyMode,
if (status == NVAPI_OK)
NvAPI_D3D_Sleep(device);
}
//-----------------------------------------------------------------------------
// Purpose: sets the latency marker
// Input : *device -
// frameNumber -
// markerType -
//-----------------------------------------------------------------------------
void GFX_SetLatencyMarker(IUnknown* device,
const NV_LATENCY_MARKER_TYPE markerType)
{
NV_LATENCY_MARKER_PARAMS params = {};
params.version = NV_LATENCY_MARKER_PARAMS_VER1;
params.frameID = s_ReflexFrameNumber;
params.markerType = markerType;
NvAPI_D3D_SetLatencyMarker(device, &params);
}

View File

@ -1,8 +1,13 @@
#ifndef GFSDK_REFLEX_H
#define GFSDK_REFLEX_H
void GFX_RunLowLatencySDK(IUnknown* device, const bool useLowLatencyMode,
int GFX_GetFrameNumber(void);
void GFX_RunLowLatencyFrame(IUnknown* device, const bool useLowLatencyMode,
const bool useLowLatencyBoost, const bool useMarkersToOptimize,
const float maxFramesPerSecond);
void GFX_SetLatencyMarker(IUnknown* device,
const NV_LATENCY_MARKER_TYPE markerType);
#endif // GFSDK_REFLEX_H

View File

@ -6,6 +6,7 @@
#include "tier1/cvar.h"
#include "windows/id3dx.h"
#include "windows/input.h"
#include "geforce/reflex.h"
#include "gameui/IConsole.h"
#include "gameui/IBrowser.h"
#include "engine/framelimit.h"
@ -143,7 +144,13 @@ HRESULT __stdcall Present(IDXGISwapChain* pSwapChain, UINT nSyncInterval, UINT n
g_FrameLimiter.Run();
DrawImGui();
///////////////////////////////////////////////////////////////////////////////
// TODO[ AMOS ]: Profile performance by placing the marker before and after the
// frame limit call!!!
GFX_SetLatencyMarker(D3D11Device(), PRESENT_START);
HRESULT result = s_fnSwapChainPresent(pSwapChain, nSyncInterval, nFlags);
GFX_SetLatencyMarker(D3D11Device(), PRESENT_END);
return result;
}