Fetching DirectX 9 Device

From Valve Developer Community
Revision as of 02:24, 4 November 2025 by Ficool2 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This tutorial shows how to fetch the DirectX 9 Device in 64-bit games on the Team Fortress 2 branch Team Fortress 2 branch. This method works on both Windows and Linux platforms.

Access to the low-level DirectX 9 device allows the creation of effects that Source has no support for, such as rendering cubemaps in real-time.

Warning.pngWarning:This is intended for advanced users. Low level access to the DirectX 9 device is very powerful, but also comes with great responsibility.

Tutorial

Open the render target initialization code in game/client/baseclientrendertargets.cpp, and it's corresponding header file baseclientrendertargets.h. This will be explained shortly.

In the header file, add the following:

struct IDirect3DDevice9;

extern IDirect3DDevice9* g_pDirect3DDevice9;

In the cpp file, add this #include:

#include "shaderapi/IShaderDevice.h"

Following that, add this definition in:

IDirect3DDevice9* g_pDirect3DDevice9 = NULL;

Now a new function will be added into this file. Paste the following function in. You do not need to worry about how this works.

static bool FindDX9Device()
{
	// Try both DXVK and DirectX 9 mode
	CreateInterfaceFn interface = Sys_GetFactory( "shaderapivk" );
	if ( !interface )
		interface = Sys_GetFactory( "shaderapidx9" );

	if ( !interface )
		return false;

	IShaderDevice* pShaderDevice = ( IShaderDevice* )interface( SHADER_DEVICE_INTERFACE_VERSION, NULL );
	if ( !pShaderDevice )
		return false;

	// Dereference the virtual table and access the IsUsingGraphics method
	byte* pIShaderDevice_IsUsingGraphics = ( byte* )( *( void*** )pShaderDevice )[5];
	// Resolve the RIP instruction to get the absolute address
	byte* ppDirect3DDevice9 = pIShaderDevice_IsUsingGraphics + 8 + *( int32* )( pIShaderDevice_IsUsingGraphics + 3 );
	g_pDirect3DDevice9 = *( IDirect3DDevice9** )ppDirect3DDevice9;

	return g_pDirect3DDevice9 != NULL;
}

Now go to the CBaseClientRenderTargets::InitClientRenderTargets function and add the following at the top:

	if ( !FindDX9Device() )
	{
		Error( "Failed to get DirectX9 device pointer" );
		return;
	}

The reason this code is being put inside render target initialization, is that the engine calls this function before the client itself even initializes. This also allows you to create custom rendertargets that aren't normally possible with the materialsystem interface.

And that's all! You can now call the raw DirectX9 API using the g_pDirect3DDevice9 interface. Include the baseclientrendertargets.h file in code that needs to use the device.

Note.pngNote:This code is independent of the client and can also be run in the shader DLLs by using a static constructor, if it's more convenient

Usage

To call functions on the DirectX 9 device, you will need to download the DirectX 9 SDK.

After you download it, extract it to a place in your mod's folder. The convention is to make a dx9sdk directory in the top-level folder of your source code.

You can then include the DirectX 9 API like-so in client code:

#include "../../dx9sdk/include/d3d9.h"

// ...
void ExampleToFetchSwapChain()
{
	IDirect3DSwapChain9* pSwapChain = NULL;
	g_pDirect3DDevice9->GetSwapChain( 0, &pSwapChain );
}
Warning.pngWarning:You might get compile errors about GetObjectA due to a macro in the Windows headers conflicting with Source's function names. To fix this, you can add #undef GetObject after the include.