読者です 読者をやめる 読者になる 読者になる

めいくりぷとのブログ

技術的なことやゲームのことやら・・・

D3D9 Texture をフックする

// IDirect3DDevice9.cpp

HRESULT APIENTRY _IDirect3DDevice9::SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture)
{
	IDirect3DDevice9 *pDevice = NULL;

	if (pTexture != NULL)
	{
		if (reinterpret_cast<_IDirect3DTexture9*>(pTexture)->GetDevice(&pDevice) == D3D_OK)
		{
			if (pDevice == this)
			{
				return m_pD3Ddev->SetTexture(Stage, reinterpret_cast<_IDirect3DTexture9*>(pTexture)->m_pD3Dtex);
			}
		}
	}

	return m_pD3Ddev->SetTexture(Stage, pTexture);
}

// IDirect3DTexture9.hpp

#ifndef _D3D9TEX_HPP
#define _D3D9TEX_HPP

#include "d3d9.hpp"

interface _IDirect3DTexture9 : public IDirect3DTexture9
{
	_IDirect3DTexture9(IDirect3DTexture9 **ppTex, IDirect3DDevice9 *pIDirect3DDevice9, UINT Width, UINT Height, D3DFORMAT Format)
	{
		m_pD3Dtex = *ppTex;
		m_pD3Ddev = pIDirect3DDevice9;
		m_Width = Width;
		m_Height = Height;
		m_Format = Format;
	}

	UINT m_Width, m_Height;
	D3DFORMAT m_Format;

	// callback interface
	IDirect3DTexture9 *m_pD3Dtex;
	IDirect3DDevice9 *m_pD3Ddev;

	// original interface
	STDMETHOD(QueryInterface)(REFIID riid, void** ppvObj);
	STDMETHOD_(ULONG, AddRef)();
	STDMETHOD_(ULONG, Release)();
	STDMETHOD(GetDevice)(IDirect3DDevice9** ppDevice);
	STDMETHOD(SetPrivateData)(REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags);
	STDMETHOD(GetPrivateData)(REFGUID refguid, void* pData, DWORD* pSizeOfData);
	STDMETHOD(FreePrivateData)(REFGUID refguid);
	STDMETHOD_(DWORD, SetPriority)(DWORD PriorityNew);
	STDMETHOD_(DWORD, GetPriority)();
	STDMETHOD_(void, PreLoad)();
	STDMETHOD_(D3DRESOURCETYPE, GetType)();
	STDMETHOD_(DWORD, SetLOD)(DWORD LODNew);
	STDMETHOD_(DWORD, GetLOD)();
	STDMETHOD_(DWORD, GetLevelCount)();
	STDMETHOD(SetAutoGenFilterType)(D3DTEXTUREFILTERTYPE FilterType);
	STDMETHOD_(D3DTEXTUREFILTERTYPE, GetAutoGenFilterType)();
	STDMETHOD_(void, GenerateMipSubLevels)();
	STDMETHOD(GetLevelDesc)(UINT Level, D3DSURFACE_DESC *pDesc);
	STDMETHOD(GetSurfaceLevel)(UINT Level, IDirect3DSurface9** ppSurfaceLevel);
	STDMETHOD(LockRect)(UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags);
	STDMETHOD(UnlockRect)(UINT Level);
	STDMETHOD(AddDirtyRect)(CONST RECT* pDirtyRect);
};

#endif

// IDirect3DTexture9.cpp
#include "stdafx.h"

#include "IDirect3DTexture9.hpp"

HRESULT APIENTRY _IDirect3DTexture9::QueryInterface(REFIID riid, void** ppvObj)
{
	return m_pD3Dtex->QueryInterface(riid, ppvObj);
}

ULONG APIENTRY _IDirect3DTexture9::AddRef()
{
	return m_pD3Dtex->AddRef();
}

ULONG APIENTRY _IDirect3DTexture9::Release()
{
	return m_pD3Dtex->Release();
}

HRESULT APIENTRY _IDirect3DTexture9::GetDevice(IDirect3DDevice9** ppDevice)
{
	*ppDevice = m_pD3Ddev;
	return D3D_OK;
}

HRESULT APIENTRY _IDirect3DTexture9::SetPrivateData(REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags)
{
	return m_pD3Dtex->SetPrivateData(refguid, pData, SizeOfData, Flags);
}

HRESULT APIENTRY _IDirect3DTexture9::GetPrivateData(REFGUID refguid, void* pData, DWORD* pSizeOfData)
{
	return m_pD3Dtex->GetPrivateData(refguid, pData, pSizeOfData);
}

HRESULT APIENTRY _IDirect3DTexture9::FreePrivateData(REFGUID refguid)
{
	return m_pD3Dtex->FreePrivateData(refguid);
}

DWORD APIENTRY _IDirect3DTexture9::SetPriority(DWORD PriorityNew)
{
	return m_pD3Dtex->SetPriority(PriorityNew);
}

DWORD APIENTRY _IDirect3DTexture9::GetPriority()
{
	return m_pD3Dtex->GetPriority();
}

void APIENTRY _IDirect3DTexture9::PreLoad()
{
	return m_pD3Dtex->PreLoad();
}

D3DRESOURCETYPE APIENTRY _IDirect3DTexture9::GetType()
{
	return m_pD3Dtex->GetType();
}

DWORD APIENTRY _IDirect3DTexture9::SetLOD(DWORD LODNew)
{
	return m_pD3Dtex->SetLOD(LODNew);
}

DWORD APIENTRY _IDirect3DTexture9::GetLOD()
{
	return m_pD3Dtex->GetLOD();
}

DWORD APIENTRY _IDirect3DTexture9::GetLevelCount()
{
	return m_pD3Dtex->GetLevelCount();
}

HRESULT APIENTRY _IDirect3DTexture9::SetAutoGenFilterType(D3DTEXTUREFILTERTYPE FilterType)
{
	return m_pD3Dtex->SetAutoGenFilterType(FilterType);
}

D3DTEXTUREFILTERTYPE APIENTRY _IDirect3DTexture9::GetAutoGenFilterType()
{
	return m_pD3Dtex->GetAutoGenFilterType();
}

void APIENTRY _IDirect3DTexture9::GenerateMipSubLevels()
{
	return m_pD3Dtex->GenerateMipSubLevels();
}

HRESULT APIENTRY _IDirect3DTexture9::GetLevelDesc(UINT Level, D3DSURFACE_DESC *pDesc)
{
	return m_pD3Dtex->GetLevelDesc(Level, pDesc);
}

HRESULT APIENTRY _IDirect3DTexture9::GetSurfaceLevel(UINT Level, IDirect3DSurface9** ppSurfaceLevel)
{
	return m_pD3Dtex->GetSurfaceLevel(Level, ppSurfaceLevel);
}

HRESULT APIENTRY _IDirect3DTexture9::LockRect(UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags)
{
	return m_pD3Dtex->LockRect(Level, pLockedRect, pRect, Flags);
}

HRESULT APIENTRY _IDirect3DTexture9::UnlockRect(UINT Level)
{
	return m_pD3Dtex->UnlockRect(Level);
}

HRESULT APIENTRY _IDirect3DTexture9::AddDirtyRect(CONST RECT* pDirtyRect)
{
	return m_pD3Dtex->AddDirtyRect(pDirtyRect);
}