NVIDIA: properly enqueue frame ID

Sync Frame ID between main thread and render thread, this changes makes the integration PASS the reflex test utility.
This commit is contained in:
Kawe Mazidjatari 2023-12-25 19:23:01 +01:00
parent 83b95d23c9
commit 735a9e0489
9 changed files with 40 additions and 49 deletions

View File

@ -15,6 +15,7 @@
#include "geforce/reflex.h"
#include "vengineclient_impl.h"
#include "cdll_engine_int.h"
#include "materialsystem/cmaterialsystem.h"
/*****************************************************************************/
#ifndef DEDICATED
@ -64,7 +65,7 @@ void FrameStageNotify_Post(const ClientFrameStage_t frameStage)
case ClientFrameStage_t::FRAME_RENDER_START:
break;
case ClientFrameStage_t::FRAME_RENDER_END:
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_END);
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_END, MaterialSystem()->GetCurrentFrameCount());
break;
case ClientFrameStage_t::FRAME_NET_FULL_FRAME_UPDATE_ON_REMOVE:
break;

View File

@ -9,10 +9,12 @@
#include "windows/id3dx.h"
#include "geforce/reflex.h"
#include "engine/gl_rsurf.h"
#include <materialsystem/cmaterialsystem.h>
void* R_DrawDepthOfField(const float a1)
{
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_START);
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_START, MaterialSystem()->GetCurrentFrameCount());
return V_DrawDepthOfField(a1);
}

View File

@ -12,6 +12,7 @@
#include "windows/id3dx.h"
#include "geforce/reflex.h"
#include "vgui/vgui_debugpanel.h"
#include <materialsystem/cmaterialsystem.h>
#endif // !DEDICATED
CCommonHostState* g_pCommonHostState = nullptr;
@ -46,7 +47,7 @@ Services environments)
void Host_CountRealTimePackets()
{
v_Host_CountRealTimePackets();
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_START);
GFX_SetLatencyMarker(D3D11Device(), SIMULATION_START, MaterialSystem()->GetCurrentFrameCount());
}
/*

View File

@ -1,6 +1,7 @@
#include "keys.h"
#include "windows/id3dx.h"
#include "geforce/reflex.h"
#include <materialsystem/cmaterialsystem.h>
KeyInfo_t* g_pKeyInfo = nullptr;
ButtonCode_t* g_pKeyEventTicks = nullptr;
@ -22,7 +23,7 @@ bool Input_Event(const InputEvent_t& inputEvent, const int noKeyUpCheck)
if (runTriggerMarker && (inputEvent.m_nType != IE_ButtonReleased || keyInfo.m_bTrapKeyUp))
{
GFX_SetLatencyMarker(D3D11Device(), TRIGGER_FLASH);
GFX_SetLatencyMarker(D3D11Device(), TRIGGER_FLASH, MaterialSystem()->GetCurrentFrameCount());
}
return v_Input_Event(inputEvent, noKeyUpCheck);

View File

@ -227,28 +227,35 @@ void CEngineAPI::RunLowLatencyFrame()
//-----------------------------------------------------------------------------
bool CEngineAPI::MainLoop()
{
#ifndef DEDICATED
bool bRunLowLatency = false;
#endif // !DEDICATED
// Main message pump
while (true)
{
// Pump messages unless someone wants to quit
if (g_pEngine->GetQuitting() != IEngine::QUIT_NOTQUITTING)
{
if (g_pEngine->GetQuitting() != IEngine::QUIT_TODESKTOP)
if (g_pEngine->GetQuitting() != IEngine::QUIT_TODESKTOP) {
return true;
}
return false;
}
#ifndef DEDICATED
CEngineAPI::RunLowLatencyFrame();
if (bRunLowLatency) {
CEngineAPI::RunLowLatencyFrame();
}
CEngineAPI::PumpMessages();
#endif // !DEDICATED
if (g_pEngine->Frame())
{
#ifndef DEDICATED
// Only increment frame number if we ran an actual engine frame.
GFX_IncrementFrameNumber();
// Only run reflex if we ran an actual engine frame.
bRunLowLatency = true;
#endif // !DEDICATED
}
}

View File

@ -16,10 +16,6 @@ bool s_ReflexModeInfoUpToDate = false;
// If not, the Low Latency SDK will not run.
NvAPI_Status s_ReflexModeUpdateStatus = NvAPI_Status::NVAPI_OK;
// Static frame number counter for latency markers.
NvU64 s_ReflexFrameNumber = 0;
NvU64 s_ReflexLastFrameNumber = 0;
//-----------------------------------------------------------------------------
// Purpose: enable/disable low latency SDK
// Input : enable -
@ -75,23 +71,6 @@ bool GFX_ParameterUpdateWasSuccessful(void)
return s_ReflexModeUpdateStatus == NvAPI_Status::NVAPI_OK;
}
//-----------------------------------------------------------------------------
// Purpose: gets the reflex frame number
// Output : int
//-----------------------------------------------------------------------------
NvU64 GFX_GetFrameNumber(void)
{
return s_ReflexFrameNumber;
}
//-----------------------------------------------------------------------------
// Purpose: increments the reflex frame number
//-----------------------------------------------------------------------------
void GFX_IncrementFrameNumber(void)
{
++s_ReflexFrameNumber;
}
//-----------------------------------------------------------------------------
// Purpose: updates the low latency parameters
// Input : *device -
@ -100,7 +79,7 @@ void GFX_IncrementFrameNumber(void)
// useMarkersToOptimize -
// maxFramesPerSecond -
//-----------------------------------------------------------------------------
void GFX_UpdateLowLatencyParameters(IUnknown* device, const bool useLowLatencyMode,
void GFX_UpdateLowLatencyParameters(IUnknown* const device, const bool useLowLatencyMode,
const bool useLowLatencyBoost, const bool useMarkersToOptimize,
const float maxFramesPerSecond)
{
@ -125,18 +104,12 @@ void GFX_UpdateLowLatencyParameters(IUnknown* device, const bool useLowLatencyMo
// Purpose: runs a frame of the low latency sdk
// Input : *device -
//-----------------------------------------------------------------------------
void GFX_RunLowLatencyFrame(IUnknown* device)
void GFX_RunLowLatencyFrame(IUnknown* const device)
{
Assert(device);
const NvU64 currentFrameNumber = GFX_GetFrameNumber();
if (s_ReflexLastFrameNumber == currentFrameNumber)
return;
if (GFX_ParameterUpdateWasSuccessful())
NvAPI_D3D_Sleep(device);
s_ReflexLastFrameNumber = currentFrameNumber;
}
//-----------------------------------------------------------------------------
@ -145,8 +118,8 @@ void GFX_RunLowLatencyFrame(IUnknown* device)
// frameNumber -
// markerType -
//-----------------------------------------------------------------------------
void GFX_SetLatencyMarker(IUnknown* device,
const NV_LATENCY_MARKER_TYPE markerType)
void GFX_SetLatencyMarker(IUnknown* const device,
const NV_LATENCY_MARKER_TYPE markerType, const NvU64 frameID)
{
Assert(device);
@ -155,9 +128,9 @@ void GFX_SetLatencyMarker(IUnknown* device,
NV_LATENCY_MARKER_PARAMS params = {};
params.version = NV_LATENCY_MARKER_PARAMS_VER1;
params.frameID = s_ReflexFrameNumber;
params.frameID = frameID;
params.markerType = markerType;
NvAPI_D3D_SetLatencyMarker(device, &params);
PCLSTATS_MARKER(markerType, s_ReflexFrameNumber);
PCLSTATS_MARKER(markerType, frameID);
}

View File

@ -7,9 +7,6 @@ bool GFX_IsLowLatencySDKEnabled(void);
void GFX_MarkLowLatencyParametersOutOfDate(void);
bool GFX_HasPendingLowLatencyParameterUpdates(void);
NvU64 GFX_GetFrameNumber(void);
void GFX_IncrementFrameNumber(void);
void GFX_UpdateLowLatencyParameters(IUnknown* device, const bool useLowLatencyMode,
const bool useLowLatencyBoost, const bool useMarkersToOptimize,
const float maxFramesPerSecond);
@ -17,6 +14,6 @@ void GFX_UpdateLowLatencyParameters(IUnknown* device, const bool useLowLatencyMo
void GFX_RunLowLatencyFrame(IUnknown* device);
void GFX_SetLatencyMarker(IUnknown* device,
const NV_LATENCY_MARKER_TYPE markerType);
const NV_LATENCY_MARKER_TYPE markerType, const NvU64 frameID);
#endif // GFSDK_REFLEX_H

View File

@ -9,6 +9,7 @@
#include "windows/id3dx.h"
#include "geforce/reflex.h"
#include "inputsystem/inputsystem.h"
#include <materialsystem/cmaterialsystem.h>
////-----------------------------------------------------------------------------
//// Enables/disables input
@ -59,7 +60,7 @@ LRESULT CInputSystem::WindowProc(void* unused, HWND hwnd, UINT uMsg, WPARAM wPar
{
if (PCLSTATS_IS_PING_MSG_ID(uMsg))
{
GFX_SetLatencyMarker(D3D11Device(), PC_LATENCY_PING);
GFX_SetLatencyMarker(D3D11Device(), PC_LATENCY_PING, MaterialSystem()->GetCurrentFrameCount());
}
}

View File

@ -115,11 +115,19 @@ void* __fastcall DispatchDrawCall(int64_t a1, uint64_t a2, int a3, int a4, int64
//---------------------------------------------------------------------------------
ssize_t SpinPresent(void)
{
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_END);
// NOTE: -1 since we need to sync this with its corresponding frame, g_FrameNum
// gets incremented in CMaterialSystem::SwapBuffers, which is after the markers
// for simulation start/end and render submit start. The render thread (here)
// continues after to finish the frame.
const NvU64 frameID = (NvU64)MaterialSystem()->GetCurrentFrameCount() -1;
GFX_SetLatencyMarker(D3D11Device(), PRESENT_START);
GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_END, frameID);
// TODO[ AMOS ]: move to actual Present runtime call? SpinPresent calls some
// other DX buffer copy API's before the actual Present calls is being made.
GFX_SetLatencyMarker(D3D11Device(), PRESENT_START, frameID);
const ssize_t val = v_SpinPresent();
GFX_SetLatencyMarker(D3D11Device(), PRESENT_END);
GFX_SetLatencyMarker(D3D11Device(), PRESENT_END, frameID);
return val;
}