Skip to content

Commit be0307f

Browse files
committed
D3D fix-ups #6 (Performance)
1 parent aca7cc0 commit be0307f

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

Client/core/DXHook/CDirect3DData.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,20 @@ void CDirect3DData::StoreTransform(D3DTRANSFORMSTATETYPE dwMatrixToStore, const
3838
if (!pMatrix)
3939
return;
4040

41-
// Use direct assignment instead of memcpy for better performance
41+
// Only copy matrix if it has changed (avoid 64-byte copy)
4242
switch (dwMatrixToStore)
4343
{
4444
case D3DTS_VIEW:
45-
m_mViewMatrix = *pMatrix;
45+
if (memcmp(&m_mViewMatrix, pMatrix, sizeof(D3DMATRIX)) != 0)
46+
m_mViewMatrix = *pMatrix;
4647
break;
4748
case D3DTS_PROJECTION:
48-
m_mProjMatrix = *pMatrix;
49+
if (memcmp(&m_mProjMatrix, pMatrix, sizeof(D3DMATRIX)) != 0)
50+
m_mProjMatrix = *pMatrix;
4951
break;
5052
case D3DTS_WORLD:
51-
m_mWorldMatrix = *pMatrix;
53+
if (memcmp(&m_mWorldMatrix, pMatrix, sizeof(D3DMATRIX)) != 0)
54+
m_mWorldMatrix = *pMatrix;
5255
break;
5356
default:
5457
// Do nothing for unsupported transforms

Client/core/DXHook/CProxyDirect3DDevice9.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <atomic>
1717
#include <cmath>
1818
#include <mutex>
19+
#include <thread>
1920
#include <game/CSettings.h>
2021

2122
class CProxyDirect3DDevice9;
@@ -26,24 +27,6 @@ void ApplyBorderlessColorCorrection(CProxyDirect3DDevice9* proxyDevice, const D3
2627
namespace
2728
{
2829

29-
constexpr const char* kSetTextureContexts[] = {
30-
"SetTexture stage 0",
31-
"SetTexture stage 1",
32-
"SetTexture stage 2",
33-
"SetTexture stage 3",
34-
"SetTexture stage 4",
35-
"SetTexture stage 5",
36-
"SetTexture stage 6",
37-
"SetTexture stage 7",
38-
};
39-
40-
const char* GetSetTextureContextString(size_t stage)
41-
{
42-
constexpr auto count = std::size(kSetTextureContexts);
43-
if (stage < count)
44-
return kSetTextureContexts[stage];
45-
return "SetTexture";
46-
}
4730

4831
} // unnamed namespace
4932

@@ -702,7 +685,7 @@ static bool WaitForGpuIdle(IDirect3DDevice9* pDevice)
702685
break;
703686
}
704687

705-
Sleep(0);
688+
std::this_thread::yield();
706689
}
707690
}
708691

@@ -841,8 +824,8 @@ HRESULT CProxyDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamet
841824

842825
HRESULT CProxyDirect3DDevice9::Present(CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
843826
{
844-
// Reset frame stat counters
845-
DeviceState.FrameStats = {};
827+
// Reset frame stat counters - using memset for efficiency
828+
memset(&DeviceState.FrameStats, 0, sizeof(DeviceState.FrameStats));
846829

847830
bool bDeviceTemporarilyLost = false;
848831
HRESULT hrCoopLevel = D3DERR_INVALIDCALL;
@@ -1152,7 +1135,6 @@ HRESULT CProxyDirect3DDevice9::BeginScene()
11521135
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
11531136
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
11541137
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
1155-
11561138
m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
11571139
m_pDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
11581140

@@ -1243,7 +1225,9 @@ HRESULT CProxyDirect3DDevice9::SetMaterial(CONST D3DMATERIAL9* pMaterial)
12431225
return D3DERR_INVALIDCALL;
12441226
}
12451227

1246-
DeviceState.Material = *pMaterial;
1228+
// Update cache if material has changed (avoid 68-byte copy)
1229+
if (memcmp(&DeviceState.Material, pMaterial, sizeof(D3DMATERIAL9)) != 0)
1230+
DeviceState.Material = *pMaterial;
12471231
return m_pDevice->SetMaterial(pMaterial);
12481232
}
12491233

@@ -1259,7 +1243,8 @@ HRESULT CProxyDirect3DDevice9::SetLight(DWORD Index, CONST D3DLIGHT9* pLight)
12591243
return D3DERR_INVALIDCALL;
12601244
}
12611245

1262-
if (Index < NUMELMS(DeviceState.Lights))
1246+
// Update cache if light has changed (avoid 104-byte copy)
1247+
if (Index < NUMELMS(DeviceState.Lights) && memcmp(&DeviceState.Lights[Index], pLight, sizeof(D3DLIGHT9)) != 0)
12631248
DeviceState.Lights[Index] = *pLight;
12641249
return m_pDevice->SetLight(Index, pLight);
12651250
}
@@ -1293,8 +1278,10 @@ HRESULT CProxyDirect3DDevice9::GetClipPlane(DWORD Index, float* pPlane)
12931278

12941279
HRESULT CProxyDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE State, DWORD Value)
12951280
{
1281+
// Update cache for state tracking
12961282
if (State < NUMELMS(DeviceState.RenderState.Raw))
12971283
DeviceState.RenderState.Raw[State] = Value;
1284+
12981285
return m_pDevice->SetRenderState(State, Value);
12991286
}
13001287

@@ -1355,9 +1342,11 @@ HRESULT CProxyDirect3DDevice9::GetTextureStageState(DWORD Stage, D3DTEXTURESTAGE
13551342

13561343
HRESULT CProxyDirect3DDevice9::SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
13571344
{
1345+
// Update cache for state tracking
13581346
if (Stage < NUMELMS(DeviceState.StageState))
13591347
if (Type < NUMELMS(DeviceState.StageState[Stage].Raw))
13601348
DeviceState.StageState[Stage].Raw[Type] = Value;
1349+
13611350
return m_pDevice->SetTextureStageState(Stage, Type, Value);
13621351
}
13631352

@@ -1368,9 +1357,11 @@ HRESULT CProxyDirect3DDevice9::GetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYP
13681357

13691358
HRESULT CProxyDirect3DDevice9::SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
13701359
{
1360+
// Update cache for state tracking
13711361
if (Sampler < NUMELMS(DeviceState.SamplerState))
13721362
if (Type < NUMELMS(DeviceState.SamplerState[Sampler].Raw))
13731363
DeviceState.SamplerState[Sampler].Raw[Type] = Value;
1364+
13741365
return m_pDevice->SetSamplerState(Sampler, Type, Value);
13751366
}
13761367

0 commit comments

Comments
 (0)