Skip to content

Commit

Permalink
Check device count in AddressLookupTable
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Aug 23, 2024
1 parent 801b10c commit 74d5141
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 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 7108
#define BUILD_NUMBER 7109
9 changes: 9 additions & 0 deletions d3d9/AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ class AddressLookupTableD3d9
}
}

UINT GetDeviceCount()
{
if (ConstructorFlag)
{
return TRUE;
}
return g_map[AddressCacheIndex<m_IDirect3DDevice9Ex>::CacheIndex].size();
}

private:
bool ConstructorFlag = false;
std::unordered_map<void*, class AddressLookupTableD3d9Object*> g_map[MaxIndex];
Expand Down
16 changes: 14 additions & 2 deletions d3d9/IDirect3D9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,13 @@ HRESULT m_IDirect3D9Ex::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND h

*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex((LPDIRECT3DDEVICE9EX)*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9);

((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetProxyLookupTable(new AddressLookupTableD3d9<m_IDirect3DDevice9Ex>(nullptr));
// Create the object and add it to the vector
ADDRESSTABLE newAddressTable = { new AddressLookupTableD3d9<m_IDirect3DDevice9Ex>(nullptr) };

// Add the newTable to the AddressDeviceMap vector
AddressDeviceMap.push_back(newAddressTable);

((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetProxyLookupTable(newAddressTable.ProxyAddressLookupTable);
((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetDeviceDetails(DeviceDetails);

return D3D_OK;
Expand Down Expand Up @@ -380,7 +386,13 @@ HRESULT m_IDirect3D9Ex::CreateDeviceEx(THIS_ UINT Adapter, D3DDEVTYPE DeviceType

*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex(*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9Ex);

((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetProxyLookupTable(new AddressLookupTableD3d9<m_IDirect3DDevice9Ex>(nullptr));
// Create the object and add it to the vector
ADDRESSTABLE newAddressTable = { new AddressLookupTableD3d9<m_IDirect3DDevice9Ex>(nullptr) };

// Add the newTable to the AddressDeviceMap vector
AddressDeviceMap.push_back(newAddressTable);

((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetProxyLookupTable(newAddressTable.ProxyAddressLookupTable);
((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetDeviceDetails(DeviceDetails);

return D3D_OK;
Expand Down
26 changes: 26 additions & 0 deletions d3d9/IDirect3D9Ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class m_IDirect3D9Ex : public IDirect3D9Ex, public AddressLookupTableD3d9Object
HRESULT CreateDeviceT(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, D3DDISPLAYMODEEX* pFullscreenDisplayMode, IDirect3DDevice9Ex** ppReturnedDeviceInterface)
{ return (ProxyInterfaceEx) ? ProxyInterfaceEx->CreateDeviceEx(Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, pFullscreenDisplayMode, ppReturnedDeviceInterface) : D3DERR_INVALIDCALL; }

// AddressTable Structure
struct ADDRESSTABLE {
AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable = nullptr;
};

// AddressDeviceMap as a vector of ADDRESSTABLE
std::vector<ADDRESSTABLE> AddressDeviceMap;

// Other helper functions
void LogAdapterNames();
void InitInterface();
Expand All @@ -40,8 +48,26 @@ class m_IDirect3D9Ex : public IDirect3D9Ex, public AddressLookupTableD3d9Object
{
LOG_LIMIT(3, __FUNCTION__ << " (" << this << ")" << " deleting interface!");

for (auto& entry : AddressDeviceMap)
{
delete entry.ProxyAddressLookupTable;
}

ReleaseInterface();
}
void ClearAddressTable(AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable)
{
// Remove device from the map
for (auto it = AddressDeviceMap.begin(); it != AddressDeviceMap.end(); ++it)
{
if (it->ProxyAddressLookupTable == ProxyAddressLookupTable)
{
AddressDeviceMap.erase(it);
delete ProxyAddressLookupTable;
break;
}
}
}

/*** IUnknown methods ***/
STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj);
Expand Down
39 changes: 1 addition & 38 deletions d3d9/IDirect3DDevice9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,6 @@
DebugOverlay DOverlay;
#endif

struct TABLEMAP {
m_IDirect3DDevice9Ex* Device = nullptr;
AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable = nullptr;
};

std::vector<TABLEMAP> AddressDeviceMap;

// Add device to map
void AddToAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable)
{
AddressDeviceMap.push_back({ Device, ProxyAddressLookupTable });
}

// Remove device from the map
void RemoveFromAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable)
{
// Remove device from the map
for (auto it = AddressDeviceMap.begin(); it != AddressDeviceMap.end(); ++it)
{
if (it->Device == Device)
{
AddressDeviceMap.erase(it);
break;
}
}
// Check if another device is using this lookup table
for (auto& entry : AddressDeviceMap)
{
if (entry.ProxyAddressLookupTable == ProxyAddressLookupTable)
{
return;
}
}
// Delete lookup table if no other device is using it
delete ProxyAddressLookupTable;
}

HRESULT m_IDirect3DDevice9Ex::QueryInterface(REFIID riid, void** ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ") " << riid;
Expand Down Expand Up @@ -118,7 +81,7 @@ ULONG m_IDirect3DDevice9Ex::Release()

if (ref == 0)
{
RemoveFromAddressDeviceMap(this, ProxyAddressLookupTable);
delete this;
}

return ref;
Expand Down
9 changes: 7 additions & 2 deletions d3d9/IDirect3DDevice9Ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class m_IDirect3DDevice9Ex : public IDirect3DDevice9Ex, public AddressLookupTabl
{
WndProc::RemoveWndProc(DeviceDetails.DeviceWindow);
}

ProxyAddressLookupTable->DeleteAddress(this);

if (ProxyAddressLookupTable->GetDeviceCount() == 0)
{
m_pD3DEx->ClearAddressTable(ProxyAddressLookupTable);
}
}
void SetProxyLookupTable(AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* LookupTable)
{
Expand All @@ -79,8 +86,6 @@ class m_IDirect3DDevice9Ex : public IDirect3DDevice9Ex, public AddressLookupTabl
ProxyAddressLookupTable = LookupTable;

ProxyAddressLookupTable->SaveAddress(this, ProxyInterface);

AddToAddressDeviceMap(this, ProxyAddressLookupTable);
}
}
void SetDeviceDetails(DEVICEDETAILS& NewDeviceDetails)
Expand Down
3 changes: 0 additions & 3 deletions d3d9/d3d9.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ struct DEVICEDETAILS
DWORD DeviceMultiSampleQuality = 0;
};

void AddToAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable);
void RemoveFromAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9<m_IDirect3DDevice9Ex>* ProxyAddressLookupTable);

DWORD UpdateBehaviorFlags(DWORD BehaviorFlags);
void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND hFocusWindow, DEVICEDETAILS& DeviceDetails, bool ForceExclusiveFullscreen, bool SetWindow);
void UpdatePresentParameterForMultisample(D3DPRESENT_PARAMETERS* pPresentationParameters, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD MultiSampleQuality);
Expand Down

0 comments on commit 74d5141

Please sign in to comment.