Skip to content

Commit

Permalink
Merge pull request #98 from elishacloud/QueryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire authored May 15, 2019
2 parents fb4e69f + 6b67f55 commit ccd6572
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 17 deletions.
1 change: 1 addition & 0 deletions d3d8to9.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<ClCompile Include="source\d3d8to9_vertex_buffer.cpp" />
<ClCompile Include="source\d3d8to9_volume.cpp" />
<ClCompile Include="source\d3d8types.cpp" />
<ClCompile Include="source\interface_query.cpp" />
<ClCompile Include="source\lookup_table.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions d3d8to9.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<ClCompile Include="source\lookup_table.cpp">
<Filter>helpers</Filter>
</ClCompile>
<ClCompile Include="source\interface_query.cpp">
<Filter>helpers</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="source\d3dx9.hpp">
Expand Down
3 changes: 3 additions & 0 deletions source/d3d8to9.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,6 @@ class Direct3DIndexBuffer8 : public Direct3DResource8, public AddressLookupTable
#ifndef D3D8TO9NOLOG
extern std::ofstream LOG;
#endif

REFIID ConvertREFIID(REFIID riid);
void genericQueryInterface(REFIID riid, LPVOID *ppvObj, Direct3DDevice8* pDevice);
2 changes: 1 addition & 1 deletion source/d3d8to9_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ HRESULT STDMETHODCALLTYPE Direct3D8::QueryInterface(REFIID riid, void **ppvObj)
return S_OK;
}

return ProxyInterface->QueryInterface(riid, ppvObj);
return ProxyInterface->QueryInterface(ConvertREFIID(riid), ppvObj);
}
ULONG STDMETHODCALLTYPE Direct3D8::AddRef()
{
Expand Down
27 changes: 21 additions & 6 deletions source/d3d8to9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ HRESULT STDMETHODCALLTYPE Direct3DDevice8::QueryInterface(REFIID riid, void **pp
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, this);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DDevice8::AddRef()
{
Expand Down Expand Up @@ -542,13 +549,21 @@ HRESULT STDMETHODCALLTYPE Direct3DDevice8::CopyRects(Direct3DSurface8 *pSourceSu

if (SourceDesc.Pool == D3DPOOL_MANAGED || DestinationDesc.Pool != D3DPOOL_DEFAULT)
{
hr = D3DERR_INVALIDCALL;
if (D3DXLoadSurfaceFromSurface != nullptr)
{
hr = D3DXLoadSurfaceFromSurface(pDestinationSurface->GetProxyInterface(), nullptr, &DestinationRect, pSourceSurface->GetProxyInterface(), nullptr, &SourceRect, D3DX_FILTER_NONE, 0);
}
else
{
hr = D3DERR_INVALIDCALL;
if (SUCCEEDED(D3DXLoadSurfaceFromSurface(pDestinationSurface->GetProxyInterface(), nullptr, &DestinationRect, pSourceSurface->GetProxyInterface(), nullptr, &SourceRect, D3DX_FILTER_NONE, 0)))
{
// Explicitly call AddDirtyRect on the surface
void *pContainer = nullptr;
if (SUCCEEDED(pDestinationSurface->GetContainer(IID_IDirect3DTexture9, &pContainer)) && pContainer)
{
IDirect3DTexture9 *pTexture = (IDirect3DTexture9*)pContainer;
pTexture->AddDirtyRect(&DestinationRect);
pTexture->Release();
}
hr = D3D_OK;
}
}
}
else if (SourceDesc.Pool == D3DPOOL_DEFAULT)
Expand Down
9 changes: 8 additions & 1 deletion source/d3d8to9_index_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ HRESULT STDMETHODCALLTYPE Direct3DIndexBuffer8::QueryInterface(REFIID riid, void
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DIndexBuffer8::AddRef()
{
Expand Down
18 changes: 16 additions & 2 deletions source/d3d8to9_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ HRESULT STDMETHODCALLTYPE Direct3DSurface8::QueryInterface(REFIID riid, void **p
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DSurface8::AddRef()
{
Expand Down Expand Up @@ -70,7 +77,14 @@ HRESULT STDMETHODCALLTYPE Direct3DSurface8::FreePrivateData(REFGUID refguid)
}
HRESULT STDMETHODCALLTYPE Direct3DSurface8::GetContainer(REFIID riid, void **ppContainer)
{
return ProxyInterface->GetContainer(riid, ppContainer);
HRESULT hr = ProxyInterface->GetContainer(ConvertREFIID(riid), ppContainer);

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppContainer, Device);
}

return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DSurface8::GetDesc(D3DSURFACE_DESC8 *pDesc)
{
Expand Down
9 changes: 8 additions & 1 deletion source/d3d8to9_swap_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ HRESULT STDMETHODCALLTYPE Direct3DSwapChain8::QueryInterface(REFIID riid, void *
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DSwapChain8::AddRef()
{
Expand Down
27 changes: 24 additions & 3 deletions source/d3d8to9_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ HRESULT STDMETHODCALLTYPE Direct3DTexture8::QueryInterface(REFIID riid, void **p
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DTexture8::AddRef()
{
Expand Down Expand Up @@ -185,7 +192,14 @@ HRESULT STDMETHODCALLTYPE Direct3DCubeTexture8::QueryInterface(REFIID riid, void
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DCubeTexture8::AddRef()
{
Expand Down Expand Up @@ -336,7 +350,14 @@ HRESULT STDMETHODCALLTYPE Direct3DVolumeTexture8::QueryInterface(REFIID riid, vo
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DVolumeTexture8::AddRef()
{
Expand Down
9 changes: 8 additions & 1 deletion source/d3d8to9_vertex_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ HRESULT STDMETHODCALLTYPE Direct3DVertexBuffer8::QueryInterface(REFIID riid, voi
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DVertexBuffer8::AddRef()
{
Expand Down
18 changes: 16 additions & 2 deletions source/d3d8to9_volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ HRESULT STDMETHODCALLTYPE Direct3DVolume8::QueryInterface(REFIID riid, void **pp
return S_OK;
}

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

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppvObj, Device);
}

return hr;
}
ULONG STDMETHODCALLTYPE Direct3DVolume8::AddRef()
{
Expand Down Expand Up @@ -70,7 +77,14 @@ HRESULT STDMETHODCALLTYPE Direct3DVolume8::FreePrivateData(REFGUID refguid)
}
HRESULT STDMETHODCALLTYPE Direct3DVolume8::GetContainer(REFIID riid, void **ppContainer)
{
return ProxyInterface->GetContainer(riid, ppContainer);
HRESULT hr = ProxyInterface->GetContainer(ConvertREFIID(riid), ppContainer);

if (SUCCEEDED(hr))
{
genericQueryInterface(riid, ppContainer, Device);
}

return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DVolume8::GetDesc(D3DVOLUME_DESC8 *pDesc)
{
Expand Down
64 changes: 64 additions & 0 deletions source/interface_query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (C) 2015 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/d3d8to9#license
*/

#define INITGUID

#include "d3d8to9.hpp"

REFIID ConvertREFIID(REFIID riid)
{
return (riid == __uuidof(Direct3D8)) ? IID_IDirect3D9 :
(riid == __uuidof(Direct3DDevice8)) ? IID_IDirect3DDevice9 :
(riid == __uuidof(Direct3DTexture8)) ? IID_IDirect3DTexture9 :
(riid == __uuidof(Direct3DCubeTexture8)) ? IID_IDirect3DCubeTexture9 :
(riid == __uuidof(Direct3DVolumeTexture8)) ? IID_IDirect3DVolumeTexture9 :
(riid == __uuidof(Direct3DVertexBuffer8)) ? IID_IDirect3DVertexBuffer9 :
(riid == __uuidof(Direct3DIndexBuffer8)) ? IID_IDirect3DIndexBuffer9 :
(riid == __uuidof(Direct3DSurface8)) ? IID_IDirect3DSurface9 :
(riid == __uuidof(Direct3DVolume8)) ? IID_IDirect3DVolume9 :
(riid == __uuidof(Direct3DSwapChain8)) ? IID_IDirect3DSwapChain9 :
riid;
}

void genericQueryInterface(REFIID riid, LPVOID *ppvObj, Direct3DDevice8* pDevice)
{
if (!ppvObj || !*ppvObj || !pDevice)
{
return;
}

if (riid == __uuidof(Direct3D8))
{
Direct3D8 *pD3D8 = nullptr;
if (SUCCEEDED(pDevice->GetDirect3D(&pD3D8)) && pD3D8)
{
*ppvObj = pD3D8;
pD3D8->Release();
return;
}
}

if (riid == __uuidof(Direct3DDevice8))
{
*ppvObj = pDevice;
return;
}

#define QUERYINTERFACE(x) \
if (riid == __uuidof(x)) \
{ \
*ppvObj = pDevice->ProxyAddressLookupTable->FindAddress<x>(*ppvObj); \
return; \
}

QUERYINTERFACE(Direct3DTexture8);
QUERYINTERFACE(Direct3DCubeTexture8);
QUERYINTERFACE(Direct3DVolumeTexture8);
QUERYINTERFACE(Direct3DVertexBuffer8);
QUERYINTERFACE(Direct3DIndexBuffer8);
QUERYINTERFACE(Direct3DSurface8);
QUERYINTERFACE(Direct3DVolume8);
QUERYINTERFACE(Direct3DSwapChain8);
}

0 comments on commit ccd6572

Please sign in to comment.