Skip to content

Commit 76eab58

Browse files
committed
Only force vsync if gfx_vsync:0
1 parent a7ef9f0 commit 76eab58

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

src/client/hook/impl/DXHooks.cpp

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,46 @@ namespace {
1818
std::shared_ptr<Hook> ExecuteCommandListsHook;
1919
}
2020

21-
void CheckTearingSupport() {
21+
// doing file read operations in DXHooks is a little silly but im too tired to find a better way
22+
bool DXHooks::IsGfxVSyncEnabled() {
23+
wchar_t userProfile[MAX_PATH];
24+
DWORD pathLen = GetEnvironmentVariableW(L"USERPROFILE", userProfile, MAX_PATH);
25+
if (pathLen == 0 || pathLen >= MAX_PATH) {
26+
return true;
27+
}
28+
29+
std::wstring optionsPath(userProfile);
30+
optionsPath +=
31+
L"\\AppData\\Local\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\LocalState\\games\\com.mojang\\minecraftpe\\options.txt";
32+
33+
std::ifstream file(optionsPath.c_str());
34+
if (!file.is_open()) {
35+
return true;
36+
}
37+
38+
std::string line;
39+
while (std::getline(file, line)) {
40+
std::erase(line, '\r');
41+
if (line.find("gfx_vsync:") == 0) {
42+
return line != "gfx_vsync:0";
43+
}
44+
}
45+
46+
// default to enabled
47+
return true;
48+
}
49+
50+
void DXHooks::CheckTearingSupport() {
2251
ComPtr<IDXGIFactory5> factory5;
2352
if (SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory5)))) {
2453
BOOL allowTearing = FALSE;
2554
if (SUCCEEDED(factory5->CheckFeatureSupport(
2655
DXGI_FEATURE_PRESENT_ALLOW_TEARING,
2756
&allowTearing,
2857
sizeof(allowTearing)))) {
29-
tearingSupported = allowTearing;
58+
if (allowTearing && !DXHooks::IsGfxVSyncEnabled()) {
59+
tearingSupported = true;
60+
}
3061
}
3162
}
3263
}
@@ -100,11 +131,11 @@ HRESULT __stdcall DXHooks::SwapChain_ResizeBuffers(
100131
}
101132

102133
HRESULT __stdcall DXHooks::CommandQueue_ExecuteCommandLists(ID3D12CommandQueue* queue, UINT NumCommandLists,
103-
ID3D12CommandList* const* ppCommandLists) {
104-
auto lock = Latite::getRenderer().lock();
105-
Latite::getRenderer().setCommandQueue(queue);
106-
return ExecuteCommandListsHook->oFunc<decltype(&CommandQueue_ExecuteCommandLists)>()(
107-
queue, NumCommandLists, ppCommandLists);
134+
ID3D12CommandList* const* ppCommandLists) {
135+
auto lock = Latite::getRenderer().lock();
136+
Latite::getRenderer().setCommandQueue(queue);
137+
return ExecuteCommandListsHook->oFunc<decltype(&CommandQueue_ExecuteCommandLists)>()(
138+
queue, NumCommandLists, ppCommandLists);
108139
}
109140

110141
DXHooks::DXHooks() : HookGroup("DirectX") {
@@ -116,7 +147,7 @@ DXHooks::DXHooks() : HookGroup("DirectX") {
116147
ComPtr<ID3D11DeviceContext> dctx;
117148
ComPtr<ID3D12CommandQueue> cqueue;
118149

119-
CheckTearingSupport();
150+
DXHooks::CheckTearingSupport();
120151

121152
ThrowIfFailed(CreateDXGIFactory(IID_PPV_ARGS(&factory)));
122153
ThrowIfFailed(factory->EnumAdapters(0, adapter.GetAddressOf()));
@@ -147,13 +178,13 @@ DXHooks::DXHooks() : HookGroup("DirectX") {
147178
swapChainDesc.OutputWindow = hWnd;
148179
swapChainDesc.SampleDesc.Count = 1;
149180
swapChainDesc.Windowed = TRUE;
150-
181+
151182
D3D_FEATURE_LEVEL lvl[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1 };
152183

153184
D3D_FEATURE_LEVEL featureLevel;
154-
auto hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, lvl, 2, D3D11_SDK_VERSION,
155-
&swapChainDesc, swapChain.GetAddressOf(), device.GetAddressOf(),
156-
&featureLevel, dctx.GetAddressOf());
185+
auto hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, lvl, 2, D3D11_SDK_VERSION,
186+
&swapChainDesc, swapChain.GetAddressOf(), device.GetAddressOf(),
187+
&featureLevel, dctx.GetAddressOf());
157188

158189
uintptr_t* vftable = *reinterpret_cast<uintptr_t**>(swapChain.Get());
159190
uintptr_t* cqueueVftable = nullptr;
@@ -168,7 +199,7 @@ DXHooks::DXHooks() : HookGroup("DirectX") {
168199
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
169200
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
170201
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
171-
202+
172203
ThrowIfFailed(device12->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cqueue)));
173204
cqueueVftable = *reinterpret_cast<uintptr_t**>(cqueue.Get());
174205
}
@@ -177,13 +208,13 @@ DXHooks::DXHooks() : HookGroup("DirectX") {
177208
DestroyWindow(hWnd);
178209
UnregisterClass(L"dummywnd", Latite::get().dllInst);
179210

180-
ComPtr<IDXGIFactory2> factory2;
181-
if (SUCCEEDED(factory.As(&factory2))) {
182-
void** vtable = *(void***)factory2.Get();
183-
MH_CreateHook(vtable[16], DXHooks::CreateSwapChainForCoreWindowHook,
184-
(void**)&origCreateSwapChain);
185-
MH_EnableHook(vtable[16]);
186-
}
211+
ComPtr<IDXGIFactory2> factory2;
212+
if (SUCCEEDED(factory.As(&factory2))) {
213+
void** vtable = *(void***)factory2.Get();
214+
MH_CreateHook(vtable[16], DXHooks::CreateSwapChainForCoreWindowHook,
215+
(void**)&origCreateSwapChain);
216+
MH_EnableHook(vtable[16]);
217+
}
187218

188219
PresentHook = addHook(vftable[8], SwapChain_Present, "IDXGISwapChain::Present");
189220
ResizeBuffersHook = addHook(vftable[13], SwapChain_ResizeBuffers, "IDXGISwapChain::ResizeBuffers");

src/client/hook/impl/DXHooks.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
class DXHooks : public HookGroup {
66
private:
7-
static HRESULT WINAPI CreateSwapChainForCoreWindowHook(IDXGIFactory2* pFactory, IUnknown* pDevice, IUnknown* pWindow, const DXGI_SWAP_CHAIN_DESC1* pDesc, IDXGIOutput* pRestrictToOutput, IDXGISwapChain1** ppSwapChain);
7+
static HRESULT WINAPI CreateSwapChainForCoreWindowHook(IDXGIFactory2* pFactory, IUnknown* pDevice, IUnknown* pWindow, const DXGI_SWAP_CHAIN_DESC1* pDesc, IDXGIOutput* pRestrictToOutput, IDXGISwapChain1** ppSwapChain);
88
static HRESULT __stdcall SwapChain_Present(IDXGISwapChain* chain, UINT SyncInterval, UINT Flags);
99
static HRESULT __stdcall SwapChain_ResizeBuffers(IDXGISwapChain* chain, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags);
1010
static HRESULT __stdcall CommandQueue_ExecuteCommandLists(ID3D12CommandQueue* queue, UINT NumCommandLists, ID3D12CommandList* const* ppCommandLists);
1111

12-
void checkTearingSupport();
12+
static void CheckTearingSupport();
13+
static bool IsGfxVSyncEnabled();
1314
public:
1415
DXHooks();
1516
};

0 commit comments

Comments
 (0)