diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 6004a450..bc95503e 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7108 +#define BUILD_NUMBER 7109 diff --git a/d3d9/AddressLookupTable.h b/d3d9/AddressLookupTable.h index a3b4e8bb..b1b98243 100644 --- a/d3d9/AddressLookupTable.h +++ b/d3d9/AddressLookupTable.h @@ -164,6 +164,15 @@ class AddressLookupTableD3d9 } } + UINT GetDeviceCount() + { + if (ConstructorFlag) + { + return TRUE; + } + return g_map[AddressCacheIndex::CacheIndex].size(); + } + private: bool ConstructorFlag = false; std::unordered_map g_map[MaxIndex]; diff --git a/d3d9/IDirect3D9Ex.cpp b/d3d9/IDirect3D9Ex.cpp index 7b6548b0..0a7f16a3 100644 --- a/d3d9/IDirect3D9Ex.cpp +++ b/d3d9/IDirect3D9Ex.cpp @@ -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(nullptr)); + // Create the object and add it to the vector + ADDRESSTABLE newAddressTable = { new AddressLookupTableD3d9(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; @@ -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(nullptr)); + // Create the object and add it to the vector + ADDRESSTABLE newAddressTable = { new AddressLookupTableD3d9(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; diff --git a/d3d9/IDirect3D9Ex.h b/d3d9/IDirect3D9Ex.h index 529cdb1f..366604ed 100644 --- a/d3d9/IDirect3D9Ex.h +++ b/d3d9/IDirect3D9Ex.h @@ -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* ProxyAddressLookupTable = nullptr; + }; + + // AddressDeviceMap as a vector of ADDRESSTABLE + std::vector AddressDeviceMap; + // Other helper functions void LogAdapterNames(); void InitInterface(); @@ -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* 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); diff --git a/d3d9/IDirect3DDevice9Ex.cpp b/d3d9/IDirect3DDevice9Ex.cpp index 57855922..3ada0d14 100644 --- a/d3d9/IDirect3DDevice9Ex.cpp +++ b/d3d9/IDirect3DDevice9Ex.cpp @@ -24,43 +24,6 @@ DebugOverlay DOverlay; #endif -struct TABLEMAP { - m_IDirect3DDevice9Ex* Device = nullptr; - AddressLookupTableD3d9* ProxyAddressLookupTable = nullptr; -}; - -std::vector AddressDeviceMap; - -// Add device to map -void AddToAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9* ProxyAddressLookupTable) -{ - AddressDeviceMap.push_back({ Device, ProxyAddressLookupTable }); -} - -// Remove device from the map -void RemoveFromAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9* 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; @@ -118,7 +81,7 @@ ULONG m_IDirect3DDevice9Ex::Release() if (ref == 0) { - RemoveFromAddressDeviceMap(this, ProxyAddressLookupTable); + delete this; } return ref; diff --git a/d3d9/IDirect3DDevice9Ex.h b/d3d9/IDirect3DDevice9Ex.h index 6c86fddb..24a64c91 100644 --- a/d3d9/IDirect3DDevice9Ex.h +++ b/d3d9/IDirect3DDevice9Ex.h @@ -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* LookupTable) { @@ -79,8 +86,6 @@ class m_IDirect3DDevice9Ex : public IDirect3DDevice9Ex, public AddressLookupTabl ProxyAddressLookupTable = LookupTable; ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); - - AddToAddressDeviceMap(this, ProxyAddressLookupTable); } } void SetDeviceDetails(DEVICEDETAILS& NewDeviceDetails) diff --git a/d3d9/d3d9.h b/d3d9/d3d9.h index 04721a4b..2f0c484b 100644 --- a/d3d9/d3d9.h +++ b/d3d9/d3d9.h @@ -46,9 +46,6 @@ struct DEVICEDETAILS DWORD DeviceMultiSampleQuality = 0; }; -void AddToAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9* ProxyAddressLookupTable); -void RemoveFromAddressDeviceMap(m_IDirect3DDevice9Ex* Device, AddressLookupTableD3d9* 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);