Skip to content

Commit

Permalink
Reduce wrapper overhead and memory
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 13, 2024
1 parent 93e5b6e commit 090e63b
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7148
#define BUILD_NUMBER 7149
2 changes: 1 addition & 1 deletion External/dinputto8
2 changes: 1 addition & 1 deletion d3d9/AddressLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void AddressLookupTableD3d9::DeleteAddress(T* Wrapper)

constexpr UINT CacheIndex = AddressCacheIndex<T>::CacheIndex;
auto it = std::find_if(g_map[CacheIndex].begin(), g_map[CacheIndex].end(),
[=](auto Map) -> bool { return Map.second == Wrapper; });
[=](auto& Map) -> bool { return Map.second == Wrapper; });

if (it != std::end(g_map[CacheIndex]))
{
Expand Down
30 changes: 23 additions & 7 deletions ddraw/AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AddressLookupTableDdraw
private:
bool ConstructorFlag = false;
std::unordered_map<void*, class AddressLookupTableDdrawObject*> g_map[MaxIndex];
std::unordered_map<class AddressLookupTableDdrawObject*, void*> reverse_map[MaxIndex]; // Reverse mapping

template <typename T>
struct AddressCacheIndex { static constexpr UINT CacheIndex = 0; };
Expand Down Expand Up @@ -309,10 +310,9 @@ class AddressLookupTableDdraw
}

constexpr UINT CacheIndex = AddressCacheIndex<T>::CacheIndex;
auto it = std::find_if(g_map[CacheIndex].begin(), g_map[CacheIndex].end(),
[=](auto Map) -> bool { return Map.second == Wrapper; });
auto it = reverse_map[CacheIndex].find(Wrapper);

if (it != std::end(g_map[CacheIndex]))
if (it != reverse_map[CacheIndex].end())
{
return true;
}
Expand All @@ -339,13 +339,23 @@ class AddressLookupTableDdraw
return false;
}

bool CheckSurfaceExists(LPDIRECTDRAWSURFACE7 lpDDSrcSurface) {
return
(IsValidWrapperAddress((m_IDirectDrawSurface*)lpDDSrcSurface) ||
IsValidWrapperAddress((m_IDirectDrawSurface2*)lpDDSrcSurface) ||
IsValidWrapperAddress((m_IDirectDrawSurface3*)lpDDSrcSurface) ||
IsValidWrapperAddress((m_IDirectDrawSurface4*)lpDDSrcSurface) ||
IsValidWrapperAddress((m_IDirectDrawSurface7*)lpDDSrcSurface));
}

template <typename T>
void SaveAddress(T *Wrapper, void *Proxy)
{
constexpr UINT CacheIndex = AddressCacheIndex<T>::CacheIndex;
if (Wrapper && Proxy)
{
g_map[CacheIndex][Proxy] = Wrapper;
reverse_map[CacheIndex][Wrapper] = Proxy; // Update reverse map
}
}

Expand All @@ -358,14 +368,20 @@ class AddressLookupTableDdraw
}

constexpr UINT CacheIndex = AddressCacheIndex<T>::CacheIndex;
auto it = std::find_if(g_map[CacheIndex].begin(), g_map[CacheIndex].end(),
[=](auto Map) -> bool { return Map.second == Wrapper; });

if (it != std::end(g_map[CacheIndex]))
// Remove from g_map
for (auto it = g_map[CacheIndex].begin(); it != g_map[CacheIndex].end(); ++it)
{
it = g_map[CacheIndex].erase(it);
if (it->second == Wrapper)
{
g_map[CacheIndex].erase(it);
break;
}
}

// Remove from reverse_map
reverse_map[CacheIndex].erase(Wrapper);

#pragma warning (push)
#pragma warning (disable : 4127)
if (CacheIndex == AddressCacheIndex<m_IDirectDrawX>::CacheIndex &&
Expand Down
54 changes: 35 additions & 19 deletions ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,28 @@ void *m_IDirect3DDeviceX::GetWrapperInterfaceX(DWORD DirectXVersion)
switch (DirectXVersion)
{
case 1:
if (!WrapperInterface)
{
WrapperInterface = new m_IDirect3DDevice((LPDIRECT3DDEVICE)ProxyInterface, this);
}
return WrapperInterface;
case 2:
if (!WrapperInterface2)
{
WrapperInterface2 = new m_IDirect3DDevice2((LPDIRECT3DDEVICE2)ProxyInterface, this);
}
return WrapperInterface2;
case 3:
if (!WrapperInterface3)
{
WrapperInterface3 = new m_IDirect3DDevice3((LPDIRECT3DDEVICE3)ProxyInterface, this);
}
return WrapperInterface3;
case 7:
if (!WrapperInterface7)
{
WrapperInterface7 = new m_IDirect3DDevice7((LPDIRECT3DDEVICE7)ProxyInterface, this);
}
return WrapperInterface7;
default:
LOG_LIMIT(100, __FUNCTION__ << " Error: wrapper interface version not found: " << DirectXVersion);
Expand Down Expand Up @@ -805,18 +821,11 @@ HRESULT m_IDirect3DDeviceX::GetTexture(DWORD dwStage, LPDIRECTDRAWSURFACE7* lplp

if (AttachedTexture[dwStage])
{
if (CheckSurfaceExists(AttachedTexture[dwStage]))
{
AttachedTexture[dwStage]->AddRef();
AttachedTexture[dwStage]->AddRef();

*lplpTexture = AttachedTexture[dwStage];
*lplpTexture = AttachedTexture[dwStage];

hr = D3D_OK;
}
else
{
AttachedTexture[dwStage] = nullptr;
}
hr = D3D_OK;
}

return hr;
Expand Down Expand Up @@ -4158,11 +4167,6 @@ HRESULT m_IDirect3DDeviceX::GetInfo(DWORD dwDevInfoID, LPVOID pDevInfoStruct, DW

void m_IDirect3DDeviceX::InitDevice(DWORD DirectXVersion)
{
WrapperInterface = new m_IDirect3DDevice((LPDIRECT3DDEVICE)ProxyInterface, this);
WrapperInterface2 = new m_IDirect3DDevice2((LPDIRECT3DDEVICE2)ProxyInterface, this);
WrapperInterface3 = new m_IDirect3DDevice3((LPDIRECT3DDEVICE3)ProxyInterface, this);
WrapperInterface7 = new m_IDirect3DDevice7((LPDIRECT3DDEVICE7)ProxyInterface, this);

if (!Config.Dd7to9)
{
return;
Expand Down Expand Up @@ -4191,10 +4195,22 @@ void m_IDirect3DDeviceX::InitDevice(DWORD DirectXVersion)

void m_IDirect3DDeviceX::ReleaseDevice()
{
WrapperInterface->DeleteMe();
WrapperInterface2->DeleteMe();
WrapperInterface3->DeleteMe();
WrapperInterface7->DeleteMe();
if (WrapperInterface)
{
WrapperInterface->DeleteMe();
}
if (WrapperInterface2)
{
WrapperInterface2->DeleteMe();
}
if (WrapperInterface3)
{
WrapperInterface3->DeleteMe();
}
if (WrapperInterface7)
{
WrapperInterface7->DeleteMe();
}

if (ddrawParent && !Config.Exiting)
{
Expand Down
31 changes: 20 additions & 11 deletions ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
REFCLSID ClassID;

// Store d3d device version wrappers
m_IDirect3DDevice* WrapperInterface;
m_IDirect3DDevice2* WrapperInterface2;
m_IDirect3DDevice3* WrapperInterface3;
m_IDirect3DDevice7* WrapperInterface7;
m_IDirect3DDevice* WrapperInterface = nullptr;
m_IDirect3DDevice2* WrapperInterface2 = nullptr;
m_IDirect3DDevice3* WrapperInterface3 = nullptr;
m_IDirect3DDevice7* WrapperInterface7 = nullptr;

// Convert Device
m_IDirectDrawX *ddrawParent = nullptr;
Expand Down Expand Up @@ -138,13 +138,6 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
inline IDirect3DDevice2 *GetProxyInterfaceV2() { return (IDirect3DDevice2 *)ProxyInterface; }
inline IDirect3DDevice3 *GetProxyInterfaceV3() { return (IDirect3DDevice3 *)ProxyInterface; }
inline IDirect3DDevice7 *GetProxyInterfaceV7() { return ProxyInterface; }
inline bool CheckSurfaceExists(LPDIRECTDRAWSURFACE7 lpDDSrcSurface) { return
(ProxyAddressLookupTable.IsValidWrapperAddress((m_IDirectDrawSurface*)lpDDSrcSurface) ||
ProxyAddressLookupTable.IsValidWrapperAddress((m_IDirectDrawSurface2*)lpDDSrcSurface) ||
ProxyAddressLookupTable.IsValidWrapperAddress((m_IDirectDrawSurface3*)lpDDSrcSurface) ||
ProxyAddressLookupTable.IsValidWrapperAddress((m_IDirectDrawSurface4*)lpDDSrcSurface) ||
ProxyAddressLookupTable.IsValidWrapperAddress((m_IDirectDrawSurface7*)lpDDSrcSurface));
}

// Interface initialization functions
void InitDevice(DWORD DirectXVersion);
Expand Down Expand Up @@ -299,6 +292,22 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
void ReleaseLightInterface(m_IDirect3DLight* lpLight);

// Functions handling the ddraw parent interface
void ClearSurface(m_IDirectDrawSurfaceX* lpSurfaceX)
{
if (lpCurrentRenderTargetX == lpSurfaceX)
{
lpCurrentRenderTargetX = nullptr;
}
for (UINT x = 1; x < MaxTextureStages; x++)
{
if (CurrentTextureSurfaceX[x] == lpSurfaceX)
{
SetTexture(x, (LPDIRECTDRAWSURFACE7)nullptr);
AttachedTexture[x] = nullptr;
CurrentTextureSurfaceX[x] = nullptr;
}
}
}
void SetDdrawParent(m_IDirectDrawX *ddraw)
{
ddrawParent = ddraw;
Expand Down
31 changes: 24 additions & 7 deletions ddraw/IDirect3DMaterialX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,22 @@ void *m_IDirect3DMaterialX::GetWrapperInterfaceX(DWORD DirectXVersion)
switch (DirectXVersion)
{
case 1:
if (!WrapperInterface)
{
WrapperInterface = new m_IDirect3DMaterial((LPDIRECT3DMATERIAL)ProxyInterface, this);
}
return WrapperInterface;
case 2:
if (!WrapperInterface2)
{
WrapperInterface2 = new m_IDirect3DMaterial2((LPDIRECT3DMATERIAL2)ProxyInterface, this);
}
return WrapperInterface2;
case 3:
if (!WrapperInterface3)
{
WrapperInterface3 = new m_IDirect3DMaterial3((LPDIRECT3DMATERIAL3)ProxyInterface, this);
}
return WrapperInterface3;
default:
LOG_LIMIT(100, __FUNCTION__ << " Error: wrapper interface version not found: " << DirectXVersion);
Expand Down Expand Up @@ -344,10 +356,6 @@ HRESULT m_IDirect3DMaterialX::Unreserve()

void m_IDirect3DMaterialX::InitMaterial(DWORD DirectXVersion)
{
WrapperInterface = new m_IDirect3DMaterial((LPDIRECT3DMATERIAL)ProxyInterface, this);
WrapperInterface2 = new m_IDirect3DMaterial2((LPDIRECT3DMATERIAL2)ProxyInterface, this);
WrapperInterface3 = new m_IDirect3DMaterial3((LPDIRECT3DMATERIAL3)ProxyInterface, this);

if (ProxyInterface)
{
return;
Expand All @@ -358,9 +366,18 @@ void m_IDirect3DMaterialX::InitMaterial(DWORD DirectXVersion)

void m_IDirect3DMaterialX::ReleaseMaterial()
{
WrapperInterface->DeleteMe();
WrapperInterface2->DeleteMe();
WrapperInterface3->DeleteMe();
if (WrapperInterface)
{
WrapperInterface->DeleteMe();
}
if (WrapperInterface2)
{
WrapperInterface2->DeleteMe();
}
if (WrapperInterface3)
{
WrapperInterface3->DeleteMe();
}

if (mHandle && D3DDeviceInterface && *D3DDeviceInterface)
{
Expand Down
6 changes: 3 additions & 3 deletions ddraw/IDirect3DMaterialX.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class m_IDirect3DMaterialX : public IUnknown, public AddressLookupTableDdrawObje
D3DMATERIALHANDLE mHandle = 0;

// Store d3d material version wrappers
m_IDirect3DMaterial *WrapperInterface;
m_IDirect3DMaterial2 *WrapperInterface2;
m_IDirect3DMaterial3 *WrapperInterface3;
m_IDirect3DMaterial *WrapperInterface = nullptr;
m_IDirect3DMaterial2 *WrapperInterface2 = nullptr;
m_IDirect3DMaterial3 *WrapperInterface3 = nullptr;

// Wrapper interface functions
inline REFIID GetWrapperType(DWORD DirectXVersion)
Expand Down
21 changes: 16 additions & 5 deletions ddraw/IDirect3DTextureX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ void *m_IDirect3DTextureX::GetWrapperInterfaceX(DWORD DirectXVersion)
switch (DirectXVersion)
{
case 1:
if (!WrapperInterface)
{
WrapperInterface = new m_IDirect3DTexture((LPDIRECT3DTEXTURE)ProxyInterface, this);
}
return WrapperInterface;
case 2:
if (!WrapperInterface2)
{
WrapperInterface2 = new m_IDirect3DTexture2((LPDIRECT3DTEXTURE2)ProxyInterface, this);
}
return WrapperInterface2;
default:
LOG_LIMIT(100, __FUNCTION__ << " Error: wrapper interface version not found: " << DirectXVersion);
Expand Down Expand Up @@ -344,9 +352,6 @@ HRESULT m_IDirect3DTextureX::Unload()

void m_IDirect3DTextureX::InitTexture(DWORD DirectXVersion)
{
WrapperInterface = new m_IDirect3DTexture((LPDIRECT3DTEXTURE)ProxyInterface, this);
WrapperInterface2 = new m_IDirect3DTexture2((LPDIRECT3DTEXTURE2)ProxyInterface, this);

if (ProxyInterface)
{
return;
Expand All @@ -357,8 +362,14 @@ void m_IDirect3DTextureX::InitTexture(DWORD DirectXVersion)

void m_IDirect3DTextureX::ReleaseTexture()
{
WrapperInterface->DeleteMe();
WrapperInterface2->DeleteMe();
if (WrapperInterface)
{
WrapperInterface->DeleteMe();
}
if (WrapperInterface2)
{
WrapperInterface2->DeleteMe();
}

if (tHandle && D3DDeviceInterface && *D3DDeviceInterface)
{
Expand Down
4 changes: 2 additions & 2 deletions ddraw/IDirect3DTextureX.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class m_IDirect3DTextureX : public IUnknown, public AddressLookupTableDdrawObjec
DWORD tHandle = 0;

// Store d3d texture version wrappers
m_IDirect3DTexture *WrapperInterface;
m_IDirect3DTexture2 *WrapperInterface2;
m_IDirect3DTexture *WrapperInterface = nullptr;
m_IDirect3DTexture2 *WrapperInterface2 = nullptr;

// Wrapper interface functions
inline REFIID GetWrapperType(DWORD DirectXVersion)
Expand Down
21 changes: 16 additions & 5 deletions ddraw/IDirect3DVertexBufferX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ void *m_IDirect3DVertexBufferX::GetWrapperInterfaceX(DWORD DirectXVersion)
switch (DirectXVersion)
{
case 1:
if (!WrapperInterface)
{
WrapperInterface = new m_IDirect3DVertexBuffer((LPDIRECT3DVERTEXBUFFER)ProxyInterface, this);
}
return WrapperInterface;
case 7:
if (!WrapperInterface7)
{
WrapperInterface7 = new m_IDirect3DVertexBuffer7((LPDIRECT3DVERTEXBUFFER7)ProxyInterface, this);
}
return WrapperInterface7;
default:
LOG_LIMIT(100, __FUNCTION__ << " Error: wrapper interface version not found: " << DirectXVersion);
Expand Down Expand Up @@ -532,9 +540,6 @@ HRESULT m_IDirect3DVertexBufferX::ProcessVerticesStrided(DWORD dwVertexOp, DWORD

void m_IDirect3DVertexBufferX::InitVertexBuffer(DWORD DirectXVersion)
{
WrapperInterface = new m_IDirect3DVertexBuffer((LPDIRECT3DVERTEXBUFFER)ProxyInterface, this);
WrapperInterface7 = new m_IDirect3DVertexBuffer7((LPDIRECT3DVERTEXBUFFER7)ProxyInterface, this);

if (!Config.Dd7to9)
{
return;
Expand All @@ -552,8 +557,14 @@ void m_IDirect3DVertexBufferX::InitVertexBuffer(DWORD DirectXVersion)

void m_IDirect3DVertexBufferX::ReleaseVertexBuffer()
{
WrapperInterface->DeleteMe();
WrapperInterface7->DeleteMe();
if (WrapperInterface)
{
WrapperInterface->DeleteMe();
}
if (WrapperInterface7)
{
WrapperInterface7->DeleteMe();
}

ReleaseD9Buffers(false, false);

Expand Down
Loading

0 comments on commit 090e63b

Please sign in to comment.