Skip to content

Commit

Permalink
Fix some QueryInterface() issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Aug 23, 2024
1 parent 03d5c7a commit 5aadda9
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 87 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 7103
#define BUILD_NUMBER 7104
73 changes: 58 additions & 15 deletions d3d9/AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,69 @@ class AddressLookupTableD3d9
template <>
struct AddressCacheIndex<m_IDirect3DVolumeTexture9> { static constexpr UINT CacheIndex = 15; };

m_IDirect3DSwapChain9Ex *CreateInterface(void *Proxy, REFIID riid)
m_IDirect3D9Ex* CreateInterface(m_IDirect3D9Ex* Proxy, void*, REFIID riid)
{
return new m_IDirect3D9Ex(static_cast<m_IDirect3D9Ex*>(Proxy), riid);
}
m_IDirect3DDevice9Ex* CreateInterface(m_IDirect3DDevice9Ex* Proxy, m_IDirect3D9Ex* Device, REFIID riid)
{
return new m_IDirect3DDevice9Ex(static_cast<m_IDirect3DDevice9Ex*>(Proxy), Device, riid);
}
m_IDirect3DCubeTexture9* CreateInterface(m_IDirect3DCubeTexture9* Proxy, void*, REFIID)
{
return new m_IDirect3DCubeTexture9(static_cast<m_IDirect3DCubeTexture9*>(Proxy), pDevice);
}
m_IDirect3DIndexBuffer9* CreateInterface(m_IDirect3DIndexBuffer9* Proxy, void*, REFIID)
{
return new m_IDirect3DIndexBuffer9(static_cast<m_IDirect3DIndexBuffer9*>(Proxy), pDevice);
}
m_IDirect3DPixelShader9* CreateInterface(m_IDirect3DPixelShader9* Proxy, void*, REFIID)
{
return new m_IDirect3DPixelShader9(static_cast<m_IDirect3DPixelShader9*>(Proxy), pDevice);
}
m_IDirect3DQuery9* CreateInterface(m_IDirect3DQuery9* Proxy, void*, REFIID)
{
return new m_IDirect3DQuery9(static_cast<m_IDirect3DQuery9*>(Proxy), pDevice);
}
m_IDirect3DStateBlock9* CreateInterface(m_IDirect3DStateBlock9* Proxy, void*, REFIID)
{
return new m_IDirect3DStateBlock9(static_cast<m_IDirect3DStateBlock9*>(Proxy), pDevice);
}
m_IDirect3DSurface9* CreateInterface(m_IDirect3DSurface9* Proxy, void*, REFIID)
{
return new m_IDirect3DSurface9(static_cast<m_IDirect3DSurface9*>(Proxy), pDevice);
}
m_IDirect3DSwapChain9Ex* CreateInterface(m_IDirect3DSwapChain9Ex* Proxy, void*, REFIID riid)
{
return new m_IDirect3DSwapChain9Ex(static_cast<m_IDirect3DSwapChain9Ex*>(Proxy), pDevice, riid);
}

template <typename T>
T *CreateInterface(void *Proxy)
m_IDirect3DTexture9* CreateInterface(m_IDirect3DTexture9* Proxy, void*, REFIID)
{
return new m_IDirect3DTexture9(static_cast<m_IDirect3DTexture9*>(Proxy), pDevice);
}
m_IDirect3DVertexBuffer9* CreateInterface(m_IDirect3DVertexBuffer9* Proxy, void*, REFIID)
{
return new m_IDirect3DVertexBuffer9(static_cast<m_IDirect3DVertexBuffer9*>(Proxy), pDevice);
}
m_IDirect3DVertexDeclaration9* CreateInterface(m_IDirect3DVertexDeclaration9* Proxy, void*, REFIID)
{
return new m_IDirect3DVertexDeclaration9(static_cast<m_IDirect3DVertexDeclaration9*>(Proxy), pDevice);
}
m_IDirect3DVertexShader9* CreateInterface(m_IDirect3DVertexShader9* Proxy, void*, REFIID)
{
return new m_IDirect3DVertexShader9(static_cast<m_IDirect3DVertexShader9*>(Proxy), pDevice);
}
m_IDirect3DVolume9* CreateInterface(m_IDirect3DVolume9* Proxy, void*, REFIID)
{
return new m_IDirect3DVolume9(static_cast<m_IDirect3DVolume9*>(Proxy), pDevice);
}
m_IDirect3DVolumeTexture9* CreateInterface(m_IDirect3DVolumeTexture9* Proxy, void*, REFIID)
{
return new T(static_cast<T *>(Proxy), pDevice);
return new m_IDirect3DVolumeTexture9(static_cast<m_IDirect3DVolumeTexture9*>(Proxy), pDevice);
}

template <typename T>
T *FindAddress(void *Proxy, REFIID riid = IID_IUnknown)
template <typename T, typename D>
T *FindAddress(void* Proxy, D* Device, REFIID riid)
{
if (!Proxy)
{
Expand All @@ -83,14 +133,7 @@ class AddressLookupTableD3d9
return static_cast<T *>(it->second);
}

if (riid == IID_IUnknown)
{
return CreateInterface<T>(Proxy);
}
else
{
return (T*)CreateInterface(Proxy, riid);
}
return CreateInterface((T*)Proxy, Device, riid);
}

template <typename T>
Expand Down
25 changes: 21 additions & 4 deletions d3d9/IDirect3D9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight, bool isW

HRESULT m_IDirect3D9Ex::QueryInterface(REFIID riid, void** ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ")";
Logging::LogDebug() << __FUNCTION__ << " (" << this << ") " << riid;

if ((riid == IID_IUnknown || riid == WrapperID) && ppvObj)
{
Expand All @@ -35,7 +35,22 @@ HRESULT m_IDirect3D9Ex::QueryInterface(REFIID riid, void** ppvObj)
return D3D_OK;
}

return ProxyInterface->QueryInterface(riid, ppvObj);
HRESULT hr = ProxyInterface->QueryInterface(riid, ppvObj);

if (SUCCEEDED(hr) && ppvObj)
{
if (riid == IID_IDirect3D9 || riid == IID_IDirect3D9Ex)
{
LOG_LIMIT(100, __FUNCTION__ << " Warning: creating a second wrapper interface: " << riid);
*ppvObj = ProxyAddressLookupTableDevice9.FindAddress<m_IDirect3D9Ex, void>(*ppvObj, nullptr, riid);
}
else
{
LOG_LIMIT(100, __FUNCTION__ << " Warning: not wrapping interface: " << riid);
}
}

return hr;
}

ULONG m_IDirect3D9Ex::AddRef()
Expand Down Expand Up @@ -280,7 +295,8 @@ HRESULT m_IDirect3D9Ex::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND h
DeviceDetails.DeviceMultiSampleType = pPresentationParameters->MultiSampleType;
DeviceDetails.DeviceMultiSampleQuality = pPresentationParameters->MultiSampleQuality;

*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex((LPDIRECT3DDEVICE9EX)*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9, DeviceDetails);
*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex((LPDIRECT3DDEVICE9EX)*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9);
((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetDeviceDetails(DeviceDetails);

return D3D_OK;
}
Expand Down Expand Up @@ -350,7 +366,8 @@ HRESULT m_IDirect3D9Ex::CreateDeviceEx(THIS_ UINT Adapter, D3DDEVTYPE DeviceType
DeviceDetails.DeviceMultiSampleType = pPresentationParameters->MultiSampleType;
DeviceDetails.DeviceMultiSampleQuality = pPresentationParameters->MultiSampleQuality;

*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex(*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9Ex, DeviceDetails);
*ppReturnedDeviceInterface = new m_IDirect3DDevice9Ex(*ppReturnedDeviceInterface, this, IID_IDirect3DDevice9Ex);
((m_IDirect3DDevice9Ex*)(*ppReturnedDeviceInterface))->SetDeviceDetails(DeviceDetails);

return D3D_OK;
}
Expand Down
6 changes: 5 additions & 1 deletion d3d9/IDirect3D9Ex.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirect3D9Ex : public IDirect3D9Ex
class m_IDirect3D9Ex : public IDirect3D9Ex, public AddressLookupTableD3d9Object
{
private:
LPDIRECT3D9 ProxyInterface;
Expand Down Expand Up @@ -30,11 +30,15 @@ class m_IDirect3D9Ex : public IDirect3D9Ex
ProxyInterfaceEx = pDirect3D;
}

ProxyAddressLookupTableDevice9.SaveAddress(this, ProxyInterface);

LogAdapterNames();
}
~m_IDirect3D9Ex()
{
LOG_LIMIT(3, __FUNCTION__ << " (" << this << ")" << " deleting interface!");

ProxyAddressLookupTableDevice9.DeleteAddress(this);
}

/*** IUnknown methods ***/
Expand Down
4 changes: 2 additions & 2 deletions d3d9/IDirect3DCubeTexture9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

HRESULT m_IDirect3DCubeTexture9::QueryInterface(THIS_ REFIID riid, void** ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ")";
Logging::LogDebug() << __FUNCTION__ << " (" << this << ") " << riid;

if ((riid == IID_IDirect3DCubeTexture9 || riid == IID_IUnknown || riid == IID_IDirect3DResource9 || riid == IID_IDirect3DBaseTexture9) && ppvObj)
{
Expand Down Expand Up @@ -175,7 +175,7 @@ HRESULT m_IDirect3DCubeTexture9::GetCubeMapSurface(THIS_ D3DCUBEMAP_FACES FaceTy

if (SUCCEEDED(hr) && ppCubeMapSurface)
{
*ppCubeMapSurface = m_pDeviceEx->ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9>(*ppCubeMapSurface);
*ppCubeMapSurface = m_pDeviceEx->ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9, m_IDirect3DDevice9Ex>(*ppCubeMapSurface, m_pDeviceEx, IID_IDirect3DTexture9);
}

return hr;
Expand Down
43 changes: 26 additions & 17 deletions d3d9/IDirect3DDevice9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DebugOverlay DOverlay;

HRESULT m_IDirect3DDevice9Ex::QueryInterface(REFIID riid, void** ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ")";
Logging::LogDebug() << __FUNCTION__ << " (" << this << ") " << riid;

if ((riid == IID_IUnknown || riid == WrapperID) && ppvObj)
{
Expand All @@ -39,9 +39,18 @@ HRESULT m_IDirect3DDevice9Ex::QueryInterface(REFIID riid, void** ppvObj)

HRESULT hr = ProxyInterface->QueryInterface(riid, ppvObj);

if (SUCCEEDED(hr))
if (SUCCEEDED(hr) && ppvObj)
{
D3d9Wrapper::genericQueryInterface(riid, ppvObj, this);
if (riid == IID_IDirect3DDevice9 || riid == IID_IDirect3DDevice9Ex)
{
LOG_LIMIT(100, __FUNCTION__ << " Warning: creating a second wrapper interface: " << riid);
*ppvObj = ProxyAddressLookupTableDevice9.FindAddress<m_IDirect3DDevice9Ex>(*ppvObj, m_pD3DEx, riid);
((m_IDirect3DDevice9Ex*)(*ppvObj))->SetDeviceDetails(DeviceDetails);
}
else
{
D3d9Wrapper::genericQueryInterface(riid, ppvObj, this);
}
}

return hr;
Expand Down Expand Up @@ -269,7 +278,7 @@ HRESULT m_IDirect3DDevice9Ex::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *p
{
CopyMemory(pPresentationParameters, &d3dpp, sizeof(D3DPRESENT_PARAMETERS));

*ppSwapChain = new m_IDirect3DSwapChain9Ex((IDirect3DSwapChain9Ex*)*ppSwapChain, this);
*ppSwapChain = new m_IDirect3DSwapChain9Ex((IDirect3DSwapChain9Ex*)*ppSwapChain, this, IID_IDirect3DSwapChain9);
return D3D_OK;
}

Expand Down Expand Up @@ -498,7 +507,7 @@ HRESULT m_IDirect3DDevice9Ex::EndStateBlock(THIS_ IDirect3DStateBlock9** ppSB)

if (SUCCEEDED(hr) && ppSB)
{
*ppSB = ProxyAddressLookupTable->FindAddress<m_IDirect3DStateBlock9>(*ppSB);
*ppSB = ProxyAddressLookupTable->FindAddress<m_IDirect3DStateBlock9, m_IDirect3DDevice9Ex>(*ppSB, this, IID_IDirect3DStateBlock9);
}

return hr;
Expand Down Expand Up @@ -533,7 +542,7 @@ HRESULT m_IDirect3DDevice9Ex::GetRenderTarget(THIS_ DWORD RenderTargetIndex, IDi

if (SUCCEEDED(hr) && ppRenderTarget)
{
*ppRenderTarget = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9>(*ppRenderTarget);
*ppRenderTarget = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9, m_IDirect3DDevice9Ex>(*ppRenderTarget, this, IID_IDirect3DSurface9);
}

return hr;
Expand Down Expand Up @@ -636,7 +645,7 @@ HRESULT m_IDirect3DDevice9Ex::GetIndices(THIS_ IDirect3DIndexBuffer9** ppIndexDa

if (SUCCEEDED(hr) && ppIndexData)
{
*ppIndexData = ProxyAddressLookupTable->FindAddress<m_IDirect3DIndexBuffer9>(*ppIndexData);
*ppIndexData = ProxyAddressLookupTable->FindAddress<m_IDirect3DIndexBuffer9, m_IDirect3DDevice9Ex>(*ppIndexData, this, IID_IDirect3DIndexBuffer9);
}

return hr;
Expand Down Expand Up @@ -829,7 +838,7 @@ HRESULT m_IDirect3DDevice9Ex::GetPixelShader(THIS_ IDirect3DPixelShader9** ppSha

if (SUCCEEDED(hr) && ppShader)
{
*ppShader = ProxyAddressLookupTable->FindAddress<m_IDirect3DPixelShader9>(*ppShader);
*ppShader = ProxyAddressLookupTable->FindAddress<m_IDirect3DPixelShader9, m_IDirect3DDevice9Ex>(*ppShader, this, IID_IDirect3DPixelShader9);
}

return hr;
Expand Down Expand Up @@ -985,7 +994,7 @@ HRESULT m_IDirect3DDevice9Ex::GetStreamSource(THIS_ UINT StreamNumber, IDirect3D

if (SUCCEEDED(hr) && ppStreamData)
{
*ppStreamData = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexBuffer9>(*ppStreamData);
*ppStreamData = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexBuffer9, m_IDirect3DDevice9Ex>(*ppStreamData, this, IID_IDirect3DVertexBuffer9);
}

return hr;
Expand All @@ -1011,7 +1020,7 @@ HRESULT m_IDirect3DDevice9Ex::GetBackBuffer(THIS_ UINT iSwapChain, UINT iBackBuf

if (SUCCEEDED(hr) && ppBackBuffer)
{
*ppBackBuffer = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9>(*ppBackBuffer);
*ppBackBuffer = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9, m_IDirect3DDevice9Ex>(*ppBackBuffer, this, IID_IDirect3DSurface9);
}

return hr;
Expand All @@ -1025,7 +1034,7 @@ HRESULT m_IDirect3DDevice9Ex::GetDepthStencilSurface(IDirect3DSurface9 **ppZSten

if (SUCCEEDED(hr) && ppZStencilSurface)
{
*ppZStencilSurface = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9>(*ppZStencilSurface);
*ppZStencilSurface = ProxyAddressLookupTable->FindAddress<m_IDirect3DSurface9, m_IDirect3DDevice9Ex>(*ppZStencilSurface, this, IID_IDirect3DSurface9);
}

return hr;
Expand All @@ -1042,13 +1051,13 @@ HRESULT m_IDirect3DDevice9Ex::GetTexture(DWORD Stage, IDirect3DBaseTexture9 **pp
switch ((*ppTexture)->GetType())
{
case D3DRTYPE_TEXTURE:
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DTexture9>(*ppTexture);
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DTexture9, m_IDirect3DDevice9Ex>(*ppTexture, this, IID_IDirect3DTexture9);
break;
case D3DRTYPE_VOLUMETEXTURE:
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DVolumeTexture9>(*ppTexture);
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DVolumeTexture9, m_IDirect3DDevice9Ex>(*ppTexture, this, IID_IDirect3DVolumeTexture9);
break;
case D3DRTYPE_CUBETEXTURE:
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DCubeTexture9>(*ppTexture);
*ppTexture = ProxyAddressLookupTable->FindAddress<m_IDirect3DCubeTexture9, m_IDirect3DDevice9Ex>(*ppTexture, this, IID_IDirect3DCubeTexture9);
break;
default:
return D3DERR_INVALIDCALL;
Expand Down Expand Up @@ -1272,7 +1281,7 @@ HRESULT m_IDirect3DDevice9Ex::GetVertexShader(THIS_ IDirect3DVertexShader9** ppS

if (SUCCEEDED(hr) && ppShader)
{
*ppShader = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexShader9>(*ppShader);
*ppShader = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexShader9, m_IDirect3DDevice9Ex>(*ppShader, this, IID_IDirect3DVertexShader9);
}

return hr;
Expand Down Expand Up @@ -1464,7 +1473,7 @@ HRESULT m_IDirect3DDevice9Ex::GetVertexDeclaration(THIS_ IDirect3DVertexDeclarat

if (SUCCEEDED(hr) && ppDecl)
{
*ppDecl = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexDeclaration9>(*ppDecl);
*ppDecl = ProxyAddressLookupTable->FindAddress<m_IDirect3DVertexDeclaration9, m_IDirect3DDevice9Ex>(*ppDecl, this, IID_IDirect3DVertexDeclaration9);
}

return hr;
Expand Down Expand Up @@ -2045,7 +2054,7 @@ HRESULT m_IDirect3DDevice9Ex::GetSwapChain(THIS_ UINT iSwapChain, IDirect3DSwapC

if (SUCCEEDED(hr))
{
*ppSwapChain = ProxyAddressLookupTable->FindAddress<m_IDirect3DSwapChain9Ex>(*ppSwapChain);
*ppSwapChain = ProxyAddressLookupTable->FindAddress<m_IDirect3DSwapChain9Ex, m_IDirect3DDevice9Ex>(*ppSwapChain, this, IID_IDirect3DSwapChain9);
}

return hr;
Expand Down
Loading

0 comments on commit 5aadda9

Please sign in to comment.