Skip to content

Commit f564fb9

Browse files
committed
move from DX9 to DX11
compile as d3d11.dll & hook device create add fake client from imgui sample as internal test
1 parent c36173c commit f564fb9

File tree

13 files changed

+251
-27
lines changed

13 files changed

+251
-27
lines changed

fake_client/dx11_win.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include "pch.h"
2+
3+
// Data
4+
static ID3D11Device* g_pd3dDevice = NULL;
5+
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
6+
static IDXGISwapChain* g_pSwapChain = NULL;
7+
static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
8+
9+
// Forward declarations of helper functions
10+
bool CreateDeviceD3D(HWND hWnd);
11+
void CleanupDeviceD3D();
12+
void CreateRenderTarget();
13+
void CleanupRenderTarget();
14+
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
15+
16+
// Main code
17+
int dx11_test()
18+
{
19+
// Create application window
20+
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"fake_client", NULL };
21+
::RegisterClassEx(&wc);
22+
HWND hwnd = ::CreateWindow(wc.lpszClassName, L"Fake DX11 client", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
23+
24+
// Initialize Direct3D
25+
if (!CreateDeviceD3D(hwnd))
26+
{
27+
CleanupDeviceD3D();
28+
::UnregisterClass(wc.lpszClassName, wc.hInstance);
29+
return 1;
30+
}
31+
32+
// Show the window
33+
::ShowWindow(hwnd, SW_SHOWDEFAULT);
34+
::UpdateWindow(hwnd);
35+
36+
// Main loop
37+
bool done = false;
38+
while (!done)
39+
{
40+
// Poll and handle messages (inputs, window resize, etc.)
41+
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
42+
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
43+
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
44+
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
45+
MSG msg;
46+
while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
47+
{
48+
::TranslateMessage(&msg);
49+
::DispatchMessage(&msg);
50+
if (msg.message == WM_QUIT)
51+
done = true;
52+
}
53+
if (done)
54+
break;
55+
56+
const float clear_color_with_alpha[4] = { 0.0f, 0.5f, 0.7f, 1.0f };
57+
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
58+
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
59+
60+
g_pSwapChain->Present(1, 0); // Present with vsync
61+
}
62+
63+
// Cleanup
64+
CleanupDeviceD3D();
65+
::DestroyWindow(hwnd);
66+
::UnregisterClass(wc.lpszClassName, wc.hInstance);
67+
68+
return 0;
69+
}
70+
71+
// Helper functions
72+
73+
bool CreateDeviceD3D(HWND hWnd)
74+
{
75+
// Setup swap chain
76+
DXGI_SWAP_CHAIN_DESC sd;
77+
ZeroMemory(&sd, sizeof(sd));
78+
sd.BufferCount = 2;
79+
sd.BufferDesc.Width = 0;
80+
sd.BufferDesc.Height = 0;
81+
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
82+
sd.BufferDesc.RefreshRate.Numerator = 60;
83+
sd.BufferDesc.RefreshRate.Denominator = 1;
84+
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
85+
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
86+
sd.OutputWindow = hWnd;
87+
sd.SampleDesc.Count = 1;
88+
sd.SampleDesc.Quality = 0;
89+
sd.Windowed = TRUE;
90+
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
91+
92+
UINT createDeviceFlags = 0;
93+
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
94+
D3D_FEATURE_LEVEL featureLevel;
95+
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
96+
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
97+
return false;
98+
99+
CreateRenderTarget();
100+
return true;
101+
}
102+
103+
void CleanupDeviceD3D()
104+
{
105+
CleanupRenderTarget();
106+
if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
107+
if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
108+
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
109+
}
110+
111+
void CreateRenderTarget()
112+
{
113+
ID3D11Texture2D* pBackBuffer;
114+
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
115+
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
116+
pBackBuffer->Release();
117+
}
118+
119+
void CleanupRenderTarget()
120+
{
121+
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
122+
}
123+
124+
// Win32 message handler
125+
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
126+
{
127+
switch (msg)
128+
{
129+
case WM_SIZE:
130+
if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
131+
{
132+
CleanupRenderTarget();
133+
g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
134+
CreateRenderTarget();
135+
}
136+
return 0;
137+
case WM_SYSCOMMAND:
138+
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
139+
return 0;
140+
break;
141+
case WM_DESTROY:
142+
::PostQuitMessage(0);
143+
return 0;
144+
}
145+
return ::DefWindowProc(hWnd, msg, wParam, lParam);
146+
}

fake_client/fake_client.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include "pch.h"
22
#include <iostream>
33

4-
int main()
4+
int dx9_test()
55
{
6-
std::cout << "fake_client start!\n";
7-
86
std::cout << "Loading d3d9.dll \n";
9-
7+
108
HMODULE d3d9md = LoadLibrary(L"d3d9.dll");
119

1210
if (!d3d9md)
@@ -46,4 +44,16 @@ int main()
4644
system("pause");
4745

4846
return 0;
47+
}
48+
49+
int main()
50+
{
51+
std::cout << "fake_client start!\n";
52+
53+
int ret = 0;
54+
//ret = dx9_test();
55+
ret = dx11_test();
56+
57+
58+
return ret;
4959
}

fake_client/fake_client.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Link>
9696
<SubSystem>Console</SubSystem>
9797
<GenerateDebugInformation>true</GenerateDebugInformation>
98+
<AdditionalDependencies>d3d11.lib;dxgi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
9899
</Link>
99100
</ItemDefinitionGroup>
100101
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -110,6 +111,7 @@
110111
<Link>
111112
<SubSystem>Console</SubSystem>
112113
<GenerateDebugInformation>true</GenerateDebugInformation>
114+
<AdditionalDependencies>d3d11.lib;dxgi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
113115
</Link>
114116
</ItemDefinitionGroup>
115117
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -129,6 +131,7 @@
129131
<EnableCOMDATFolding>true</EnableCOMDATFolding>
130132
<OptimizeReferences>true</OptimizeReferences>
131133
<GenerateDebugInformation>true</GenerateDebugInformation>
134+
<AdditionalDependencies>d3d11.lib;dxgi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
132135
</Link>
133136
</ItemDefinitionGroup>
134137
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -149,12 +152,14 @@
149152
<EnableCOMDATFolding>true</EnableCOMDATFolding>
150153
<OptimizeReferences>true</OptimizeReferences>
151154
<GenerateDebugInformation>true</GenerateDebugInformation>
155+
<AdditionalDependencies>d3d11.lib;dxgi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
152156
</Link>
153157
</ItemDefinitionGroup>
154158
<ItemGroup>
155159
<ClInclude Include="pch.h" />
156160
</ItemGroup>
157161
<ItemGroup>
162+
<ClCompile Include="dx11_win.cpp" />
158163
<ClCompile Include="fake_client.cpp" />
159164
<ClCompile Include="pch.cpp">
160165
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

fake_client/fake_client.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
<ClCompile Include="fake_client.cpp">
2727
<Filter>sources</Filter>
2828
</ClCompile>
29+
<ClCompile Include="dx11_win.cpp">
30+
<Filter>sources</Filter>
31+
</ClCompile>
2932
</ItemGroup>
3033
</Project>

fake_client/pch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// TODO: add headers that you want to pre-compile here
55
#include <Windows.h>
66
#include <d3d9.h>
7+
#include <d3d11.h>
8+
9+
int dx11_test();
710

811

912
#endif //PCH_H

include/gw2al_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef void(*gw2al_api_event_handler)(void* data);
4545
#define GW2AL_CORE_FUNN_CLIENT_UNLOAD 13
4646
#define GW2AL_CORE_FUNN_LOG_TEXT 14
4747
#define GW2AL_CORE_FUNN_D3DCREATE_HOOK 15
48+
#define GW2AL_CORE_FUNN_D3D11CREATE_HOOK 16
4849

4950
typedef enum gw2al_log_level {
5051
LL_INFO = 0,

loader_core/d3d11_dll_impl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "stdafx.h"
2+
3+
extern "C" HRESULT WINAPI D3D11CreateDevice(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext)
4+
{
5+
return D3D11CreateDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, nullptr, nullptr, ppDevice, pFeatureLevel, ppImmediateContext);
6+
}
7+
8+
extern "C" HRESULT WINAPI D3D11CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext)
9+
{
10+
return loader_core::instance.OnD3D11CreateDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
11+
}

loader_core/deffile

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
LIBRARY d3d9.dll
1+
LIBRARY d3d11.dll
22
EXPORTS
3-
Direct3DCreate9 @30
4-
Direct3DCreate9Ex
5-
D3DPERF_BeginEvent
6-
D3DPERF_EndEvent
7-
D3DPERF_SetMarker
8-
D3DPERF_SetRegion
9-
D3DPERF_QueryRepeatFrame
10-
D3DPERF_SetOptions
11-
D3DPERF_GetStatus
3+
D3D11CreateDevice
4+
D3D11CreateDeviceAndSwapChain

loader_core/loader_core.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ void loader_core::log_text_fmt(gw2al_log_level level, const wchar_t * source, co
8787
gw2al_core__log_text(level, (wchar_t*)source, buf);
8888
}
8989

90+
void loader_core::innerInit()
91+
{
92+
if (SwitchState(LDR_ADDON_LOAD))
93+
{
94+
bool isFirstLoad = gw2al_core__init();
95+
96+
LOG_INFO(L"core", L"Addon loader v%u.%u (%S) %S", LOADER_CORE_VER_MAJOR, LOADER_CORE_VER_MINOR, LOADER_CORE_VER_NAME,
97+
isFirstLoad ? "initialized" : "reinit");
98+
99+
LoadAddonsFromDir(L"addons");
100+
101+
SwitchState(LDR_INGAME);
102+
}
103+
else
104+
LOG_WARNING(L"core", L"D3DCreate called twice without proper unload. If your addon is not working, make sure you handle this situation!");
105+
}
106+
90107
IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)
91108
{
92109
IDirect3D9* (*d3d9_create_hook)() = (IDirect3D9 * (*)())gw2al_core__query_function(GW2AL_CORE_FUNN_D3DCREATE_HOOK);
@@ -120,22 +137,46 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)
120137
}
121138

122139
IDirect3D9 * loader_core::OnD3DCreate(UINT sdkVer)
140+
{
141+
innerInit();
142+
return RouteD3DCreate(sdkVer);
143+
}
144+
145+
HRESULT loader_core::RouteD3D11CreateDeviceAndSwapChain(DX11_CREATE_FDEF)
123146
{
124-
if (SwitchState(LDR_ADDON_LOAD))
147+
typedef HRESULT (WINAPI* D3D11CreateDeviceAndSwapChainFunc)(DX11_CREATE_FDEF);
148+
D3D11CreateDeviceAndSwapChainFunc d3d11_create_hook = (D3D11CreateDeviceAndSwapChainFunc)gw2al_core__query_function(GW2AL_CORE_FUNN_D3DCREATE_HOOK);
149+
HRESULT ret = NULL;
150+
151+
if (d3d11_create_hook)
125152
{
126-
bool isFirstLoad = gw2al_core__init();
153+
LOG_DEBUG(L"core", L"Calling D3D11Create, hook = 0x%016llX", d3d11_create_hook);
154+
ret = d3d11_create_hook(DX11_CREATE_PLIST);
155+
}
156+
else
157+
{
158+
LOG_DEBUG(L"core", L"Loading system d3d11.dll");
127159

128-
LOG_INFO(L"core", L"Addon loader v%u.%u (%S) %S", LOADER_CORE_VER_MAJOR, LOADER_CORE_VER_MINOR, LOADER_CORE_VER_NAME,
129-
isFirstLoad ? "initialized" : "reinit");
160+
wchar_t infoBuf[4096];
161+
GetSystemDirectory(infoBuf, 4096);
162+
lstrcatW(infoBuf, L"\\d3d11.dll");
130163

131-
LoadAddonsFromDir(L"addons");
164+
HMODULE sys_d3d11 = LoadLibrary(infoBuf);
132165

133-
SwitchState(LDR_INGAME);
166+
167+
D3D11CreateDeviceAndSwapChainFunc origDX11Create = (D3D11CreateDeviceAndSwapChainFunc)GetProcAddress(sys_d3d11, "D3D11CreateDeviceAndSwapChain");
168+
ret = origDX11Create(DX11_CREATE_PLIST);
134169
}
135-
else
136-
LOG_WARNING(L"core", L"D3DCreate called twice without proper unload. If your addon is not working, make sure you handle this situation!");
137170

138-
return RouteD3DCreate(sdkVer);
171+
LOG_DEBUG(L"core", L"ID3D11Device = 0x%016llX", *ppDevice);
172+
173+
return ret;
174+
}
175+
176+
HRESULT loader_core::OnD3D11CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext)
177+
{
178+
innerInit();
179+
return RouteD3D11CreateDeviceAndSwapChain(DX11_CREATE_PLIST);
139180
}
140181

141182
void loader_core::SignalUnload()

loader_core/loader_core.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ typedef enum loader_state {
1414
LDR_UNLOADED
1515
} loader_state;
1616

17+
#define DX11_CREATE_FDEF IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext
18+
#define DX11_CREATE_PLIST pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext
19+
1720
class loader_core {
1821

1922
public:
@@ -22,6 +25,8 @@ class loader_core {
2225

2326
IDirect3D9* RouteD3DCreate(UINT sdkVer);
2427
IDirect3D9* OnD3DCreate(UINT sdkVer);
28+
HRESULT RouteD3D11CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext);
29+
HRESULT OnD3D11CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext);
2530
void SignalUnload();
2631

2732
loader_state GetCurrentState();
@@ -33,6 +38,7 @@ class loader_core {
3338
static loader_core instance;
3439

3540
private:
41+
void innerInit();
3642
BOOL SwitchState(loader_state newState);
3743
loader_state state;
3844

0 commit comments

Comments
 (0)