aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/latencyflex.cpp
blob: 84b79d339d3e2751d883e00f450af73baead3903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "pch.h"
#include "latencyflex.h"
#include "hookutils.h"
#include "dedicated.h"
#include "convar.h"

typedef void(*OnRenderStartType)();
OnRenderStartType OnRenderStart;

ConVar* Cvar_r_latencyflex;

HMODULE m_lfxModule{};
typedef void (*PFN_winelfx_WaitAndBeginFrame)();
PFN_winelfx_WaitAndBeginFrame m_winelfx_WaitAndBeginFrame{};

void OnRenderStartHook()
{
	if (Cvar_r_latencyflex->m_nValue)
		m_winelfx_WaitAndBeginFrame();

	OnRenderStart();
}

void InitialiseLatencyFleX(HMODULE baseAddress)
{
	if (IsDedicated())
		return;

	// Connect to the LatencyFleX service
	// LatencyFleX is an open source vendor agnostic replacement for Nvidia Reflex input latency reduction technology.
	// https://ishitatsuyuki.github.io/post/latencyflex/
	m_lfxModule = LoadLibraryA("latencyflex_wine.dll");

	if (m_lfxModule == nullptr)
	{
		spdlog::info("Unable to load LatencyFleX library, LatencyFleX disabled.");
		return;
	}

	m_winelfx_WaitAndBeginFrame = reinterpret_cast<PFN_winelfx_WaitAndBeginFrame>(reinterpret_cast<void*>(GetProcAddress(m_lfxModule,"winelfx_WaitAndBeginFrame")));
	spdlog::info("LatencyFleX initialized.");

	Cvar_r_latencyflex = RegisterConVar("r_latencyflex", "1", FCVAR_ARCHIVE, "Whether or not to use LatencyFleX input latency reduction.");

	HookEnabler hook;
	ENABLER_CREATEHOOK(hook, (char*)baseAddress + 0x1952C0, &OnRenderStartHook, reinterpret_cast<LPVOID*>(&OnRenderStart));
}