summaryrefslogtreecommitdiff
path: root/common/dx_proxy/dx_proxy.h
blob: 2dd9122728a1267754308e4c974c1e36097074a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Make dynamic loading of dx_proxy.dll and methods acquisition easier.
//
// $NoKeywords: $
//
//=============================================================================//

#ifndef DX_PROXY_H
#define DX_PROXY_H

#ifdef _WIN32
#pragma once
#endif


/*

class DxProxyModule

Uses a lazy-load technique to load the dx_proxy.dll module and acquire the
function pointers.

The dx_proxy.dll module is automatically unloaded during desctruction.

*/
class DxProxyModule
{
public:
	/// Construction
	DxProxyModule( void );
	/// Destruction
	~DxProxyModule( void );

private: // Prevent copying via copy constructor or assignment
	DxProxyModule( const DxProxyModule & );
	DxProxyModule & operator = ( const DxProxyModule & );

public:
	/// Loads the module and acquires function pointers, returns if the module was
	/// loaded successfully.
	/// If the module was already loaded the call has no effect and returns TRUE.
	BOOL Load( void );
	/// Frees the loaded module.
	void Free( void );

private:
	enum Func {
		fnD3DXCompileShaderFromFile = 0,
		fnTotal
	};
	HMODULE m_hModule;				//!< The handle of the loaded dx_proxy.dll
	FARPROC m_arrFuncs[fnTotal];	//!< The array of loaded function pointers


	///
	/// Interface functions calling into DirectX proxy
	///
public:
	HRESULT D3DXCompileShaderFromFile(
		LPCSTR                          pSrcFile,
		CONST D3DXMACRO*                pDefines,
		LPD3DXINCLUDE                   pInclude,
		LPCSTR                          pFunctionName,
		LPCSTR                          pProfile,
		DWORD                           Flags,
		LPD3DXBUFFER*                   ppShader,
		LPD3DXBUFFER*                   ppErrorMsgs,
		LPD3DXCONSTANTTABLE*            ppConstantTable );
};







//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
//     IMPLEMENTATION
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


inline DxProxyModule::DxProxyModule( void )
{
	m_hModule = NULL;
	ZeroMemory( m_arrFuncs, sizeof( m_arrFuncs ) );
}

inline DxProxyModule::~DxProxyModule( void )
{
	Free();
}

inline BOOL DxProxyModule::Load( void )
{
	if ( (m_hModule == NULL) &&
		( m_hModule = ::LoadLibrary( "dx_proxy.dll" ) ) != NULL )
	{
		// Requested function names array
		LPCSTR const arrFuncNames[fnTotal] = {
			"Proxy_D3DXCompileShaderFromFile"
		};

		// Acquire the functions
		for ( int k = 0; k < fnTotal; ++ k )
		{
			m_arrFuncs[k] = ::GetProcAddress( m_hModule, arrFuncNames[k] );
		}
	}

	return !!m_hModule;
}

inline void DxProxyModule::Free( void )
{
	if ( m_hModule )
	{
		::FreeLibrary( m_hModule );
		m_hModule = NULL;
		ZeroMemory( m_arrFuncs, sizeof( m_arrFuncs ) );
	}
}


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
//     INTERFACE FUNCTIONS IMPLEMENTATION
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////



inline HRESULT DxProxyModule::D3DXCompileShaderFromFile(
								  LPCSTR                          pSrcFile,
								  CONST D3DXMACRO*                pDefines,
								  LPD3DXINCLUDE                   pInclude,
								  LPCSTR                          pFunctionName,
								  LPCSTR                          pProfile,
								  DWORD                           Flags,
								  LPD3DXBUFFER*                   ppShader,
								  LPD3DXBUFFER*                   ppErrorMsgs,
								  LPD3DXCONSTANTTABLE*            ppConstantTable )
{
	if ( !Load() )
		return MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, 1 );
	if ( !m_arrFuncs[fnD3DXCompileShaderFromFile] )
		return MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, 2 );

	return
		( *
			( HRESULT (WINAPI *)
				( LPCSTR, CONST D3DXMACRO*, LPD3DXINCLUDE,
				  LPCSTR, LPCSTR, DWORD, LPD3DXBUFFER*,
				  LPD3DXBUFFER*, LPD3DXCONSTANTTABLE* )
			)
			m_arrFuncs[fnD3DXCompileShaderFromFile]
		)
		( pSrcFile, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable );
}


#endif // #ifndef DX_PROXY_H