Skip to content

Commit 36a69ab

Browse files
committed
base for dxgi wrapping
1 parent 50a1d3d commit 36a69ab

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

addon/com_wrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ IDXGISwapChain* wrap_CreateSwapchain(IDXGISwapChain* origSwc)
143143
return (IDXGISwapChain*)ret;
144144
}
145145

146+
void* wrap_CreateDXGI(void* origDXGI)
147+
{
148+
return origDXGI;
149+
}
150+
146151
void wrap_SwitchMethod(d3d9_vtable_method method, vtable_wrap_mode mode)
147152
{
148153
g_main_vtable[method] = wrap_vtable_arr[mode][method];

addon/com_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct wrapped_com_obj {
1414
IDirect3DDevice9* orig_dev;
1515
ID3D11Device5* orig_dev11;
1616
IDXGISwapChain* orig_swc;
17+
IDXGIFactory* orig_dxgi;
1718
};
1819
} wrapped_com_obj;
1920
#pragma pack(pop)
@@ -92,6 +93,7 @@ IDirect3DDevice9* wrap_CreateDevice(IDirect3DDevice9* origDev);
9293
IDirect3D9* wrap_CreateObj(IDirect3D9* origObj);
9394
ID3D11Device5* wrap_CreateDevice11(ID3D11Device5* origDev);
9495
IDXGISwapChain* wrap_CreateSwapchain(IDXGISwapChain* origSwc);
96+
void* wrap_CreateDXGI(void* origDXGI);
9597

9698
typedef struct wrap_event_data {
9799
void* ret;

addon/d3d9_wrapper.cpp

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ gw2al_addon_dsc gAddonDsc = {
1919
gAddonDeps
2020
};
2121

22-
HMODULE custom_d3d_module;
22+
HMODULE custom_d3d9_module;
23+
HMODULE custom_d3d11_module;
24+
HMODULE custom_dxgi_module;
2325
gw2al_hashed_name eventEnableProcName;
2426

2527
gw2al_core_vtable* instance::api = NULL;
@@ -35,22 +37,22 @@ IDirect3D9* OnD3D9Create()
3537
if (GetD3D9CustomLib)
3638
{
3739
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"Loading custom lib from D3D_wrapper_custom_d3d9_lib_query");
38-
custom_d3d_module = LoadLibraryW(GetD3D9CustomLib());
40+
custom_d3d9_module = LoadLibraryW(GetD3D9CustomLib());
3941
}
4042
else {
4143
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"Loading system d3d9");
42-
custom_d3d_module = LoadLibraryW(infoBuf);
44+
custom_d3d9_module = LoadLibraryW(infoBuf);
4345
}
4446

45-
if (!custom_d3d_module)
47+
if (!custom_d3d9_module)
4648
{
4749
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"d3d9 library failed to load");
4850
return 0;
4951
}
5052

5153
typedef IDirect3D9* (WINAPI* Direct3DCreate9Func)(UINT sdkver);
5254

53-
Direct3DCreate9Func d3d9create_fun = (Direct3DCreate9Func)GetProcAddress(custom_d3d_module, "Direct3DCreate9");
55+
Direct3DCreate9Func d3d9create_fun = (Direct3DCreate9Func)GetProcAddress(custom_d3d9_module, "Direct3DCreate9");
5456

5557
if (!d3d9create_fun)
5658
{
@@ -85,22 +87,22 @@ HRESULT OnD3D11Create(DX11_CREATE_FDEF)
8587
if (GetD3D11CustomLib)
8688
{
8789
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"Loading custom lib from D3D_wrapper_custom_d3d11_lib_query");
88-
custom_d3d_module = LoadLibraryW(GetD3D11CustomLib());
90+
custom_d3d11_module = LoadLibraryW(GetD3D11CustomLib());
8991
}
9092
else {
9193
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"Loading system d3d11");
92-
custom_d3d_module = LoadLibraryW(infoBuf);
94+
custom_d3d11_module = LoadLibraryW(infoBuf);
9395
}
9496

95-
if (!custom_d3d_module)
97+
if (!custom_d3d11_module)
9698
{
9799
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"d3d11 library failed to load");
98100
return 0;
99101
}
100102

101103
typedef HRESULT (WINAPI* D3D11CreateDeviceAndSwapChainFunc)(DX11_CREATE_FDEF);
102104

103-
D3D11CreateDeviceAndSwapChainFunc d3d11create_fun = (D3D11CreateDeviceAndSwapChainFunc)GetProcAddress(custom_d3d_module, "D3D11CreateDeviceAndSwapChain");
105+
D3D11CreateDeviceAndSwapChainFunc d3d11create_fun = (D3D11CreateDeviceAndSwapChainFunc)GetProcAddress(custom_d3d11_module, "D3D11CreateDeviceAndSwapChain");
104106

105107
if (!d3d11create_fun)
106108
{
@@ -128,6 +130,73 @@ HRESULT OnD3D11Create(DX11_CREATE_FDEF)
128130
return res;
129131
}
130132

133+
static int insideDXGICreate = 0;
134+
135+
HRESULT OnDXGICreate(UINT ver, UINT Flags, REFIID riid, void** ppFactory)
136+
{
137+
typedef HRESULT(WINAPI* DXGIFactoryCreate0)(REFIID riid, void** ppFactory);
138+
typedef HRESULT(WINAPI* DXGIFactoryCreate1)(REFIID riid, void** ppFactory);
139+
typedef HRESULT(WINAPI* DXGIFactoryCreate2)(UINT Flags, REFIID riid, void** ppFactory);
140+
141+
wchar_t infoBuf[4096];
142+
GetSystemDirectory(infoBuf, 4096);
143+
lstrcatW(infoBuf, L"\\dxgi.dll");
144+
145+
wchar_t* (*GetDXGICustomLib)() = (wchar_t* (*)())gAPI->query_function(gAPI->hash_name(L"D3D_wrapper_custom_dxgi_lib_query"));
146+
147+
148+
if (GetDXGICustomLib)
149+
{
150+
gAPI->log_text(LL_INFO, L"d3d9_wrapper", L"Loading custom lib from D3D_wrapper_custom_dxgi_lib_query");
151+
custom_dxgi_module = LoadLibraryW(GetDXGICustomLib());
152+
}
153+
else {
154+
gAPI->log_text(LL_INFO, L"d3d9_wrapper", L"Loading system dxgi");
155+
custom_dxgi_module = LoadLibraryW(infoBuf);
156+
}
157+
158+
if (!custom_dxgi_module)
159+
{
160+
gAPI->log_text(LL_INFO, L"d3d9_wrapper", L"dxgi library failed to load");
161+
return 0;
162+
}
163+
164+
165+
DXGIFactoryCreate0 origDXGI0 = (DXGIFactoryCreate0)GetProcAddress(custom_dxgi_module, "CreateDXGIFactory");
166+
DXGIFactoryCreate1 origDXGI1 = (DXGIFactoryCreate1)GetProcAddress(custom_dxgi_module, "CreateDXGIFactory1");
167+
DXGIFactoryCreate2 origDXGI2 = (DXGIFactoryCreate2)GetProcAddress(custom_dxgi_module, "CreateDXGIFactory2");
168+
169+
HRESULT ret;
170+
171+
++insideDXGICreate;
172+
switch (ver)
173+
{
174+
case 0:
175+
ret = origDXGI0(riid, ppFactory);
176+
break;
177+
case 1:
178+
ret = origDXGI1(riid, ppFactory);
179+
break;
180+
case 2:
181+
ret = origDXGI2(Flags, riid, ppFactory);
182+
break;
183+
}
184+
--insideDXGICreate;
185+
186+
if (ret != S_OK)
187+
{
188+
gAPI->log_text(LL_INFO, (wchar_t*)L"d3d9_wrapper", (wchar_t*)L"CreateDXGIFactory failed");
189+
return 0;
190+
}
191+
192+
if (insideDXGICreate == 0)
193+
{
194+
*ppFactory = wrap_CreateDXGI(*ppFactory);
195+
}
196+
197+
return ret;
198+
}
199+
131200
gw2al_addon_dsc* gw2addon_get_description()
132201
{
133202
return &gAddonDsc;
@@ -254,6 +323,7 @@ gw2al_api_ret gw2addon_load(gw2al_core_vtable* core_api)
254323

255324
gAPI->register_function(&OnD3D9Create, GW2AL_CORE_FUNN_D3DCREATE_HOOK);
256325
gAPI->register_function(&OnD3D11Create, GW2AL_CORE_FUNN_D3D11CREATE_HOOK);
326+
gAPI->register_function(&OnDXGICreate, GW2AL_CORE_FUNN_DXGICREATE_HOOK);
257327
gAPI->register_function(&d3d9_wrapper_enable_event, eventEnableProcName);
258328

259329
wrap_InitEvents();
@@ -322,8 +392,8 @@ gw2al_api_ret gw2addon_unload(int gameExiting)
322392
gAPI->unregister_function(eventEnableProcName);
323393
gAPI->unregister_function(GW2AL_CORE_FUNN_D3DCREATE_HOOK);
324394

325-
if (custom_d3d_module)
326-
FreeLibrary(custom_d3d_module);
395+
if (custom_d3d11_module)
396+
FreeLibrary(custom_d3d11_module);
327397

328398
return GW2AL_OK;
329399
}

include/gw2al_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef void(*gw2al_api_event_handler)(void* data);
5353
#define GW2AL_CORE_FUNN_LOG_TEXT 15
5454
#define GW2AL_CORE_FUNN_D3DCREATE_HOOK 16
5555
#define GW2AL_CORE_FUNN_D3D11CREATE_HOOK 17
56+
#define GW2AL_CORE_FUNN_DXGICREATE_HOOK 18
5657

5758
typedef enum gw2al_log_level {
5859
LL_INFO = 0,

0 commit comments

Comments
 (0)