From 0251c59227d4e6229ab4c5ea2183e202165ae176 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 25 Dec 2023 19:23:01 +0100 Subject: [PATCH] NVIDIA: properly enqueue frame ID Sync Frame ID between main thread and render thread, this changes makes the integration PASS the reflex test utility. --- src/engine/client/cdll_engine_int.cpp | 3 +- src/engine/gl_rsurf.cpp | 4 ++- src/engine/host.cpp | 3 +- src/engine/keys.cpp | 3 +- src/engine/sys_dll2.cpp | 15 +++++++--- src/geforce/reflex.cpp | 39 ++++---------------------- src/geforce/reflex.h | 5 +--- src/inputsystem/inputsystem.cpp | 3 +- src/materialsystem/cmaterialsystem.cpp | 14 +++++++-- 9 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/engine/client/cdll_engine_int.cpp b/src/engine/client/cdll_engine_int.cpp index c6acb0e4..a412d110 100644 --- a/src/engine/client/cdll_engine_int.cpp +++ b/src/engine/client/cdll_engine_int.cpp @@ -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; diff --git a/src/engine/gl_rsurf.cpp b/src/engine/gl_rsurf.cpp index 695526b5..0bfd7beb 100644 --- a/src/engine/gl_rsurf.cpp +++ b/src/engine/gl_rsurf.cpp @@ -9,10 +9,12 @@ #include "windows/id3dx.h" #include "geforce/reflex.h" #include "engine/gl_rsurf.h" +#include void* R_DrawDepthOfField(const float a1) { - GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_START); + GFX_SetLatencyMarker(D3D11Device(), RENDERSUBMIT_START, MaterialSystem()->GetCurrentFrameCount()); + return V_DrawDepthOfField(a1); } diff --git a/src/engine/host.cpp b/src/engine/host.cpp index f605ce81..e1b83133 100644 --- a/src/engine/host.cpp +++ b/src/engine/host.cpp @@ -12,6 +12,7 @@ #include "windows/id3dx.h" #include "geforce/reflex.h" #include "vgui/vgui_debugpanel.h" +#include #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()); } /* diff --git a/src/engine/keys.cpp b/src/engine/keys.cpp index ea120c6e..92d4b43e 100644 --- a/src/engine/keys.cpp +++ b/src/engine/keys.cpp @@ -1,6 +1,7 @@ #include "keys.h" #include "windows/id3dx.h" #include "geforce/reflex.h" +#include 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); diff --git a/src/engine/sys_dll2.cpp b/src/engine/sys_dll2.cpp index b9937ada..1911b1f7 100644 --- a/src/engine/sys_dll2.cpp +++ b/src/engine/sys_dll2.cpp @@ -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 } } diff --git a/src/geforce/reflex.cpp b/src/geforce/reflex.cpp index 824c4fde..56947b5a 100644 --- a/src/geforce/reflex.cpp +++ b/src/geforce/reflex.cpp @@ -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, ¶ms); - PCLSTATS_MARKER(markerType, s_ReflexFrameNumber); + PCLSTATS_MARKER(markerType, frameID); } diff --git a/src/geforce/reflex.h b/src/geforce/reflex.h index 3244629f..acef73b4 100644 --- a/src/geforce/reflex.h +++ b/src/geforce/reflex.h @@ -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 diff --git a/src/inputsystem/inputsystem.cpp b/src/inputsystem/inputsystem.cpp index 01f2a39a..caefb8ec 100644 --- a/src/inputsystem/inputsystem.cpp +++ b/src/inputsystem/inputsystem.cpp @@ -9,6 +9,7 @@ #include "windows/id3dx.h" #include "geforce/reflex.h" #include "inputsystem/inputsystem.h" +#include ////----------------------------------------------------------------------------- //// 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()); } } diff --git a/src/materialsystem/cmaterialsystem.cpp b/src/materialsystem/cmaterialsystem.cpp index 851fcd79..0f466392 100644 --- a/src/materialsystem/cmaterialsystem.cpp +++ b/src/materialsystem/cmaterialsystem.cpp @@ -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; }