Skip to content

Commit 0f1cac6

Browse files
committed
make performance fix from 1ba43ce actually work, and a few minor tweaks
1 parent 66811fa commit 0f1cac6

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

Client/core/DXHook/CProxyDirect3D9.cpp

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,20 @@ CProxyDirect3D9::CProxyDirect3D9(IDirect3D9* pInterface)
102102
if (!IsValidComInterfacePointer(pInterface, ComPtrValidation::ValidationMode::ForceRefresh))
103103
{
104104
SString message;
105-
message.Format("CProxyDirect3D9 ctor: received invalid IDirect3D9 pointer %p", pInterface);
105+
message.Format("CProxyDirect3D9 ctor: received invalid IDirect3D9 pointer %p, proxy will be non-functional", pInterface);
106106
AddReportLog(8753, message, 5);
107-
m_pDevice = pInterface;
108-
}
109-
else
110-
{
111-
ReplaceInterface(m_pDevice, pInterface, "CProxyDirect3D9 ctor");
112-
pInterface->Release();
107+
// Leave m_pDevice as nullptr - do not store invalid pointers
108+
return;
113109
}
114110

111+
// Take ownership of the interface pointer
112+
// ReplaceInterface will AddRef(), giving us our own reference
113+
// Caller retains their reference
114+
ReplaceInterface(m_pDevice, pInterface, "CProxyDirect3D9 ctor");
115+
115116
if (m_pDevice)
116117
{
117-
if (IsValidComInterfacePointer(m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
118+
if (IsValidComInterfacePointer(m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
118119
{
119120
ms_CreatedDirect3D9List.push_back(m_pDevice);
120121
}
@@ -137,11 +138,8 @@ CProxyDirect3D9::~CProxyDirect3D9()
137138
/*** IUnknown methods ***/
138139
HRESULT CProxyDirect3D9::QueryInterface(REFIID riid, void** ppvObj)
139140
{
140-
if (!m_pDevice || !IsValidComInterfacePointer(m_pDevice))
141+
if (!m_pDevice)
141142
{
142-
SString message;
143-
message.Format("CProxyDirect3D9::QueryInterface rejected invalid IDirect3D9 pointer %p", m_pDevice);
144-
AddReportLog(8752, message, 5);
145143
if (ppvObj)
146144
*ppvObj = nullptr;
147145
return E_POINTER;
@@ -155,18 +153,7 @@ ULONG CProxyDirect3D9::AddRef()
155153
LONG lNewRefCount = InterlockedIncrement(&m_lRefCount);
156154

157155
if (m_pDevice)
158-
{
159-
if (IsValidComInterfacePointer(m_pDevice))
160-
{
161-
m_pDevice->AddRef();
162-
}
163-
else
164-
{
165-
SString message;
166-
message.Format("CProxyDirect3D9::AddRef rejected invalid IDirect3D9 pointer %p", m_pDevice);
167-
AddReportLog(8752, message, 5);
168-
}
169-
}
156+
m_pDevice->AddRef();
170157

171158
return static_cast<ULONG>(lNewRefCount);
172159
}
@@ -184,18 +171,7 @@ ULONG CProxyDirect3D9::Release()
184171
}
185172

186173
if (m_pDevice && lNewRefCount > 0)
187-
{
188-
if (IsValidComInterfacePointer(m_pDevice))
189-
{
190-
m_pDevice->Release();
191-
}
192-
else
193-
{
194-
SString message;
195-
message.Format("CProxyDirect3D9::Release rejected invalid IDirect3D9 pointer %p", m_pDevice);
196-
AddReportLog(8752, message, 5);
197-
}
198-
}
174+
m_pDevice->Release();
199175

200176
if (lNewRefCount == 0)
201177
delete this;
@@ -206,69 +182,95 @@ ULONG CProxyDirect3D9::Release()
206182
/*** IDirect3D9 methods ***/
207183
HRESULT CProxyDirect3D9::RegisterSoftwareDevice(void* pInitializeFunction)
208184
{
185+
if (!m_pDevice)
186+
return D3DERR_INVALIDDEVICE;
209187
return m_pDevice->RegisterSoftwareDevice(pInitializeFunction);
210188
}
211189

212190
UINT CProxyDirect3D9::GetAdapterCount()
213191
{
192+
if (!m_pDevice)
193+
return 0;
214194
return m_pDevice->GetAdapterCount();
215195
}
216196

217197
HRESULT CProxyDirect3D9::GetAdapterIdentifier(UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier)
218198
{
199+
if (!m_pDevice)
200+
return D3DERR_INVALIDDEVICE;
219201
return m_pDevice->GetAdapterIdentifier(Adapter, Flags, pIdentifier);
220202
}
221203

222204
UINT CProxyDirect3D9::GetAdapterModeCount(UINT Adapter, D3DFORMAT Format)
223205
{
206+
if (!m_pDevice)
207+
return 0;
224208
return m_pDevice->GetAdapterModeCount(Adapter, Format);
225209
}
226210

227211
HRESULT CProxyDirect3D9::EnumAdapterModes(UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode)
228212
{
213+
if (!m_pDevice)
214+
return D3DERR_INVALIDDEVICE;
229215
return m_pDevice->EnumAdapterModes(Adapter, Format, Mode, pMode);
230216
}
231217

232218
HRESULT CProxyDirect3D9::GetAdapterDisplayMode(UINT Adapter, D3DDISPLAYMODE* pMode)
233219
{
220+
if (!m_pDevice)
221+
return D3DERR_INVALIDDEVICE;
234222
return m_pDevice->GetAdapterDisplayMode(Adapter, pMode);
235223
}
236224

237225
HRESULT CProxyDirect3D9::CheckDeviceType(UINT Adapter, D3DDEVTYPE DevType, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, BOOL bWindowed)
238226
{
227+
if (!m_pDevice)
228+
return D3DERR_INVALIDDEVICE;
239229
return m_pDevice->CheckDeviceType(Adapter, DevType, AdapterFormat, BackBufferFormat, bWindowed);
240230
}
241231

242232
HRESULT CProxyDirect3D9::CheckDeviceFormat(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType,
243233
D3DFORMAT CheckFormat)
244234
{
235+
if (!m_pDevice)
236+
return D3DERR_INVALIDDEVICE;
245237
return m_pDevice->CheckDeviceFormat(Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
246238
}
247239

248240
HRESULT CProxyDirect3D9::CheckDeviceMultiSampleType(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed,
249241
D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels)
250242
{
243+
if (!m_pDevice)
244+
return D3DERR_INVALIDDEVICE;
251245
return m_pDevice->CheckDeviceMultiSampleType(Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, pQualityLevels);
252246
}
253247

254248
HRESULT CProxyDirect3D9::CheckDepthStencilMatch(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat,
255249
D3DFORMAT DepthStencilFormat)
256250
{
251+
if (!m_pDevice)
252+
return D3DERR_INVALIDDEVICE;
257253
return m_pDevice->CheckDepthStencilMatch(Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
258254
}
259255

260256
HRESULT CProxyDirect3D9::CheckDeviceFormatConversion(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat)
261257
{
258+
if (!m_pDevice)
259+
return D3DERR_INVALIDDEVICE;
262260
return m_pDevice->CheckDeviceFormatConversion(Adapter, DeviceType, SourceFormat, TargetFormat);
263261
}
264262

265263
HRESULT CProxyDirect3D9::GetDeviceCaps(UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps)
266264
{
265+
if (!m_pDevice)
266+
return D3DERR_INVALIDDEVICE;
267267
return m_pDevice->GetDeviceCaps(Adapter, DeviceType, pCaps);
268268
}
269269

270270
HMONITOR CProxyDirect3D9::GetAdapterMonitor(UINT Adapter)
271271
{
272+
if (!m_pDevice)
273+
return NULL;
272274
return m_pDevice->GetAdapterMonitor(Adapter);
273275
}
274276

@@ -369,6 +371,13 @@ HRESULT CProxyDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND
369371
InvalidateRect(hFocusWindow, nullptr, TRUE);
370372
UpdateWindow(hFocusWindow);
371373

374+
// Check if proxy has valid device pointer
375+
if (!m_pDevice)
376+
{
377+
AddReportLog(8754, SStringX("CProxyDirect3D9::CreateDevice - proxy has no valid IDirect3D9 device"), 5);
378+
return D3DERR_INVALIDDEVICE;
379+
}
380+
372381
// Detect if second call to CreateDevice
373382
if (CreateDeviceSecondCallCheck(hResult, m_pDevice, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface))
374383
{
@@ -870,13 +879,19 @@ void AddCapsReport(UINT Adapter, IDirect3D9* pDirect3D, IDirect3DDevice9* pD3DDe
870879
{
871880
// Log graphic card name
872881
D3DADAPTER_IDENTIFIER9 AdapterIdent1;
873-
pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent1);
882+
ZeroMemory(&AdapterIdent1, sizeof(AdapterIdent1));
883+
hr = pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent1);
884+
if (FAILED(hr))
885+
WriteDebugEvent(SString("Warning: pDirect3D->GetAdapterIdentifier failed: %08x", hr));
874886
WriteDebugEvent("pDirect3D:");
875887
WriteDebugEvent(ToString(AdapterIdent1));
876888

877889
// Log graphic card name
878890
D3DADAPTER_IDENTIFIER9 AdapterIdent2;
879-
pDirect3DOther->GetAdapterIdentifier(Adapter, 0, &AdapterIdent2);
891+
ZeroMemory(&AdapterIdent2, sizeof(AdapterIdent2));
892+
hr = pDirect3DOther->GetAdapterIdentifier(Adapter, 0, &AdapterIdent2);
893+
if (FAILED(hr))
894+
WriteDebugEvent(SString("Warning: pDirect3DOther->GetAdapterIdentifier failed: %08x", hr));
880895
WriteDebugEvent("pDirect3DOther:");
881896
WriteDebugEvent(ToString(AdapterIdent2));
882897

@@ -1033,7 +1048,10 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
10331048
{
10341049
// Log graphic card name
10351050
D3DADAPTER_IDENTIFIER9 AdapterIdent;
1036-
pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent);
1051+
ZeroMemory(&AdapterIdent, sizeof(AdapterIdent));
1052+
HRESULT hr = pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent);
1053+
if (FAILED(hr))
1054+
WriteDebugEvent(SString("Warning: GetAdapterIdentifier failed: %08x", hr));
10371055
WriteDebugEvent(ToString(AdapterIdent));
10381056

10391057
uint uiCurrentStatus = 0; // 0-unknown 1-fail 2-success after retry 3-success
@@ -1346,7 +1364,10 @@ HRESULT CCore::OnPostCreateDevice(HRESULT hResult, IDirect3D9* pDirect3D, UINT A
13461364

13471365
// Log graphic card name
13481366
D3DADAPTER_IDENTIFIER9 AdapterIdent;
1349-
pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent);
1367+
ZeroMemory(&AdapterIdent, sizeof(AdapterIdent));
1368+
HRESULT hr = pDirect3D->GetAdapterIdentifier(Adapter, 0, &AdapterIdent);
1369+
if (FAILED(hr))
1370+
WriteDebugEvent(SString("Warning: GetAdapterIdentifier failed: %08x", hr));
13501371
WriteDebugEvent(ToString(AdapterIdent));
13511372

13521373
// Store the rendering window in the direct 3d data

Client/core/DXHook/CProxyDirect3DDevice9.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ CProxyDirect3DDevice9::CProxyDirect3DDevice9(IDirect3DDevice9* pDevice)
372372
{
373373
D3DADAPTER_IDENTIFIER9 adaptIdent;
374374
ZeroMemory(&adaptIdent, sizeof(D3DADAPTER_IDENTIFIER9));
375-
pD3D9->GetAdapterIdentifier(iAdapter, 0, &adaptIdent);
375+
HRESULT hr = pD3D9->GetAdapterIdentifier(iAdapter, 0, &adaptIdent);
376+
if (FAILED(hr))
377+
WriteDebugEvent(SString("Warning: GetAdapterIdentifier failed in device constructor: %08x", hr));
376378

377379
int iVideoCardMemoryKBTotal = GetWMIVideoAdapterMemorySize(adaptIdent.VendorId, adaptIdent.DeviceId) / 1024;
378380

0 commit comments

Comments
 (0)