From 6f6d859fe4a3931c2674fe2b0809bca8508a536d Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Fri, 8 Sep 2017 21:43:11 -0700 Subject: [PATCH] Update Ref Count Update ref count to fix issue with GTA and Serious Sam. This update stops adding a ref in each constructor and releasing in each destructor. It also stops releasing after calling 'GetType()' in the 'GetTexture' function. --- source/d3d8to9_base.cpp | 11 ----- source/d3d8to9_device.cpp | 6 --- source/d3d8to9_index_buffer.cpp | 24 +---------- source/d3d8to9_surface.cpp | 24 +---------- source/d3d8to9_swap_chain.cpp | 24 +---------- source/d3d8to9_texture.cpp | 72 ++------------------------------ source/d3d8to9_vertex_buffer.cpp | 24 +---------- source/d3d8to9_volume.cpp | 24 +---------- source/lookup_table.cpp | 2 +- source/lookup_table.hpp | 8 +--- 10 files changed, 10 insertions(+), 209 deletions(-) diff --git a/source/d3d8to9_base.cpp b/source/d3d8to9_base.cpp index fed2c96..f0f2104 100644 --- a/source/d3d8to9_base.cpp +++ b/source/d3d8to9_base.cpp @@ -13,9 +13,6 @@ static const D3DFORMAT AdapterFormats[] = { D3DFMT_A1R5G5B5 }; -IDirect3DDevice9 *pCurrentDeviceInterface = nullptr; -D3DPRESENT_PARAMETERS CurrentPresentParams; - // IDirect3D8 Direct3D8::Direct3D8(IDirect3D9 *ProxyInterface) : ProxyInterface(ProxyInterface) @@ -206,19 +203,11 @@ HRESULT STDMETHODCALLTYPE Direct3D8::CreateDevice(UINT Adapter, D3DDEVTYPE Devic HRESULT hr = ProxyInterface->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &PresentParams, &DeviceInterface); - if (hr == D3DERR_DEVICELOST && pCurrentDeviceInterface) - { - pCurrentDeviceInterface->Reset(&CurrentPresentParams); - hr = ProxyInterface->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &PresentParams, &DeviceInterface); - } - if (FAILED(hr)) { return hr; } - pCurrentDeviceInterface = DeviceInterface; - CopyMemory(&PresentParams, &CurrentPresentParams, sizeof(PresentParams)); *ppReturnedDeviceInterface = new Direct3DDevice8(this, DeviceInterface, (PresentParams.Flags & D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL) != 0); return D3D_OK; diff --git a/source/d3d8to9_device.cpp b/source/d3d8to9_device.cpp index 52febb5..a9cd552 100644 --- a/source/d3d8to9_device.cpp +++ b/source/d3d8to9_device.cpp @@ -18,15 +18,12 @@ struct VertexShaderInfo Direct3DDevice8::Direct3DDevice8(Direct3D8 *d3d, IDirect3DDevice9 *ProxyInterface, BOOL EnableZBufferDiscarding) : D3D(d3d), ProxyInterface(ProxyInterface), ZBufferDiscarding(EnableZBufferDiscarding) { - D3D->AddRef(); ProxyAddressLookupTable = new AddressLookupTable(this); PaletteFlag = SupportsPalettes(); } Direct3DDevice8::~Direct3DDevice8() { delete ProxyAddressLookupTable; - ProxyInterface->Release(); - D3D->Release(); } HRESULT STDMETHODCALLTYPE Direct3DDevice8::QueryInterface(REFIID riid, void **ppvObj) @@ -861,11 +858,8 @@ HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetTexture(DWORD Stage, Direct3DBaseT *ppTexture = ProxyAddressLookupTable->FindAddress(CubeTextureInterface); break; default: - BaseTextureInterface->Release(); return D3DERR_INVALIDCALL; } - - BaseTextureInterface->Release(); } return D3D_OK; diff --git a/source/d3d8to9_index_buffer.cpp b/source/d3d8to9_index_buffer.cpp index 937b050..1143576 100644 --- a/source/d3d8to9_index_buffer.cpp +++ b/source/d3d8to9_index_buffer.cpp @@ -9,20 +9,10 @@ Direct3DIndexBuffer8::Direct3DIndexBuffer8(Direct3DDevice8 *Device, IDirect3DIndexBuffer9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DIndexBuffer8::~Direct3DIndexBuffer8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DIndexBuffer8::QueryInterface(REFIID riid, void **ppvObj) @@ -51,19 +41,7 @@ ULONG STDMETHODCALLTYPE Direct3DIndexBuffer8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DIndexBuffer8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DIndexBuffer8::GetDevice(Direct3DDevice8 **ppDevice) diff --git a/source/d3d8to9_surface.cpp b/source/d3d8to9_surface.cpp index c4cf10b..627e6e4 100644 --- a/source/d3d8to9_surface.cpp +++ b/source/d3d8to9_surface.cpp @@ -9,20 +9,10 @@ Direct3DSurface8::Direct3DSurface8(Direct3DDevice8 *Device, IDirect3DSurface9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DSurface8::~Direct3DSurface8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DSurface8::QueryInterface(REFIID riid, void **ppvObj) @@ -50,19 +40,7 @@ ULONG STDMETHODCALLTYPE Direct3DSurface8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DSurface8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DSurface8::GetDevice(Direct3DDevice8 **ppDevice) diff --git a/source/d3d8to9_swap_chain.cpp b/source/d3d8to9_swap_chain.cpp index c1af694..4cc1780 100644 --- a/source/d3d8to9_swap_chain.cpp +++ b/source/d3d8to9_swap_chain.cpp @@ -9,20 +9,10 @@ Direct3DSwapChain8::Direct3DSwapChain8(Direct3DDevice8 *Device, IDirect3DSwapChain9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DSwapChain8::~Direct3DSwapChain8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DSwapChain8::QueryInterface(REFIID riid, void **ppvObj) @@ -50,19 +40,7 @@ ULONG STDMETHODCALLTYPE Direct3DSwapChain8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DSwapChain8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DSwapChain8::Present(const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride, const RGNDATA *pDirtyRegion) diff --git a/source/d3d8to9_texture.cpp b/source/d3d8to9_texture.cpp index cd1e909..1825e51 100644 --- a/source/d3d8to9_texture.cpp +++ b/source/d3d8to9_texture.cpp @@ -9,20 +9,10 @@ Direct3DTexture8::Direct3DTexture8(Direct3DDevice8 *Device, IDirect3DTexture9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DTexture8::~Direct3DTexture8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DTexture8::QueryInterface(REFIID riid, void **ppvObj) @@ -52,19 +42,7 @@ ULONG STDMETHODCALLTYPE Direct3DTexture8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DTexture8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DTexture8::GetDevice(Direct3DDevice8 **ppDevice) @@ -182,20 +160,10 @@ Direct3DCubeTexture8::Direct3DCubeTexture8(Direct3DDevice8 *device, IDirect3DCub ProxyInterface(ProxyInterface), Device(device) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DCubeTexture8::~Direct3DCubeTexture8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DCubeTexture8::QueryInterface(REFIID riid, void **ppvObj) @@ -225,19 +193,7 @@ ULONG STDMETHODCALLTYPE Direct3DCubeTexture8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DCubeTexture8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DCubeTexture8::GetDevice(Direct3DDevice8 **ppDevice) @@ -355,20 +311,10 @@ Direct3DVolumeTexture8::Direct3DVolumeTexture8(Direct3DDevice8 *device, IDirect3 ProxyInterface(ProxyInterface), Device(device) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DVolumeTexture8::~Direct3DVolumeTexture8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DVolumeTexture8::QueryInterface(REFIID riid, void **ppvObj) @@ -398,19 +344,7 @@ ULONG STDMETHODCALLTYPE Direct3DVolumeTexture8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DVolumeTexture8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DVolumeTexture8::GetDevice(Direct3DDevice8 **ppDevice) diff --git a/source/d3d8to9_vertex_buffer.cpp b/source/d3d8to9_vertex_buffer.cpp index 535b683..83622bd 100644 --- a/source/d3d8to9_vertex_buffer.cpp +++ b/source/d3d8to9_vertex_buffer.cpp @@ -9,20 +9,10 @@ Direct3DVertexBuffer8::Direct3DVertexBuffer8(Direct3DDevice8 *Device, IDirect3DVertexBuffer9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DVertexBuffer8::~Direct3DVertexBuffer8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DVertexBuffer8::QueryInterface(REFIID riid, void **ppvObj) @@ -51,19 +41,7 @@ ULONG STDMETHODCALLTYPE Direct3DVertexBuffer8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DVertexBuffer8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DVertexBuffer8::GetDevice(Direct3DDevice8 **ppDevice) diff --git a/source/d3d8to9_volume.cpp b/source/d3d8to9_volume.cpp index 306ce32..77564d9 100644 --- a/source/d3d8to9_volume.cpp +++ b/source/d3d8to9_volume.cpp @@ -9,20 +9,10 @@ Direct3DVolume8::Direct3DVolume8(Direct3DDevice8 *Device, IDirect3DVolume9 *ProxyInterface) : Device(Device), ProxyInterface(ProxyInterface) { - Device->AddRef(); Device->ProxyAddressLookupTable->SaveAddress(this, ProxyInterface); } Direct3DVolume8::~Direct3DVolume8() { - if (CleanUpFlag) - { - Device->ProxyAddressLookupTable->DeleteAddress(this); - if (Active) - { - Active = false; - Device->Release(); - } - } } HRESULT STDMETHODCALLTYPE Direct3DVolume8::QueryInterface(REFIID riid, void **ppvObj) @@ -50,19 +40,7 @@ ULONG STDMETHODCALLTYPE Direct3DVolume8::AddRef() } ULONG STDMETHODCALLTYPE Direct3DVolume8::Release() { - const ULONG LastRefCount = ProxyInterface->Release(); - - if (LastRefCount == 0) - { - if (Active) - { - Active = false; - Device->Release(); - } - //delete this; - } - - return LastRefCount; + return ProxyInterface->Release(); } HRESULT STDMETHODCALLTYPE Direct3DVolume8::GetDevice(Direct3DDevice8 **ppDevice) diff --git a/source/lookup_table.cpp b/source/lookup_table.cpp index 640f4ec..ee47f4c 100644 --- a/source/lookup_table.cpp +++ b/source/lookup_table.cpp @@ -19,7 +19,7 @@ AddressLookupTable::~AddressLookupTable() { assert(AddressCache[i].back().Address8 != nullptr); - AddressCache[i].back().Address8->DeleteMe(false); + AddressCache[i].back().Address8->DeleteMe(); AddressCache[i].pop_back(); } diff --git a/source/lookup_table.hpp b/source/lookup_table.hpp index 0f49830..22790c9 100644 --- a/source/lookup_table.hpp +++ b/source/lookup_table.hpp @@ -120,14 +120,8 @@ class AddressLookupTableObject public: virtual ~AddressLookupTableObject() { } - void DeleteMe(bool CleanUp = true) + void DeleteMe() { - CleanUpFlag = CleanUp; - delete this; } - -protected: - bool Active = true; - bool CleanUpFlag = true; };