1616#include < atomic>
1717#include < cmath>
1818#include < mutex>
19+ #include < thread>
1920#include < game/CSettings.h>
2021
2122class CProxyDirect3DDevice9 ;
@@ -26,24 +27,6 @@ void ApplyBorderlessColorCorrection(CProxyDirect3DDevice9* proxyDevice, const D3
2627namespace
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
842825HRESULT 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
12941279HRESULT 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
13561343HRESULT 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
13691358HRESULT 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