aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Samples/SampleFramework/renderer/src/d3d9/D3D9Renderer.h
blob: a64dcb8e6489d56de3d01cc6020e80417a9622d0 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
#ifndef D3D9_RENDERER_H
#define D3D9_RENDERER_H

#include <RendererConfig.h>

#if defined(RENDERER_ENABLE_DIRECT3D9)

#include <Renderer.h>
#include <vector>

#if defined(RENDERER_DEBUG)
	//#define D3D_DEBUG_INFO 1
#endif

#if defined(RENDERER_XBOX360)
#include <xtl.h>
#endif
#include <d3d9.h>
#include <d3dx9.h>

// Enables/Disables use of non-managed resources for vertex/instance buffers.
// Disabled for now as it appears to actually slow us down significantly on particles.
#if defined(RENDERER_XBOX360)
#define RENDERER_ENABLE_DYNAMIC_VB_POOLS 0
#define RENDERER_ENABLE_DIRECT3D9_SWAPCHAIN 0
#else
#define RENDERER_ENABLE_DYNAMIC_VB_POOLS 1
#define RENDERER_ENABLE_DIRECT3D9_SWAPCHAIN 1
#endif
struct IDirect3DSwapChain9;

namespace SampleRenderer
{

	class RendererDesc;
	class RendererColor;

	void convertToD3D9(D3DCOLOR &dxcolor, const RendererColor &color);
	void convertToD3D9(float *dxvec, const PxVec3 &vec);
	void convertToD3D9(D3DMATRIX &dxmat, const physx::PxMat44 &mat);
	void convertToD3D9(D3DMATRIX &dxmat, const RendererProjection &mat);

	class D3D9RendererResource;

	class D3D9Renderer : public Renderer
	{
		friend class D3D9RendererResource;
	public:
		class D3DXInterface
		{
			friend class D3D9Renderer;
		private:
			D3DXInterface(void);
			~D3DXInterface(void);

		public:
			HRESULT CompileShaderFromFileA(LPCSTR srcFile, CONST D3DXMACRO *defines, LPD3DXINCLUDE include, LPCSTR functionName, LPCSTR profile, DWORD flags, LPD3DXBUFFER *shader, LPD3DXBUFFER *errorMsgs, LPD3DXCONSTANTTABLE *constantTable);
			HRESULT GetShaderConstantTable(const DWORD* pFunction, LPD3DXCONSTANTTABLE *constantTable);
			LPCSTR  GetVertexShaderProfile(LPDIRECT3DDEVICE9 device);
			LPCSTR  GetPixelShaderProfile(LPDIRECT3DDEVICE9 device);
			HRESULT SaveSurfaceToFileInMemory(LPD3DXBUFFER *ppDestBuf, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY *pSrcPalette, const RECT *pSrcRect);
			HRESULT CreateBuffer(DWORD NumBytes, LPD3DXBUFFER *ppBuffer);
			HRESULT SaveSurfaceToFileA( LPCSTR pDestFile, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DSURFACE9 pSrcSurface, CONST PALETTEENTRY* pSrcPalette, CONST RECT*  pSrcRect);

		public:
#if defined(RENDERER_WINDOWS) 
			HMODULE  m_library;
			HMODULE  m_compiler_library;

#define DEFINE_D3DX_FUNCTION(_name, _return, _params) \
	typedef _return (WINAPI* LP##_name) _params;      \
	LP##_name m_##_name;

			DEFINE_D3DX_FUNCTION(D3DXCompileShaderFromFileA, HRESULT, (LPCSTR, CONST D3DXMACRO*, LPD3DXINCLUDE, LPCSTR, LPCSTR, DWORD, LPD3DXBUFFER*, LPD3DXBUFFER*, LPD3DXCONSTANTTABLE *))
			DEFINE_D3DX_FUNCTION(D3DXGetVertexShaderProfile, LPCSTR, (LPDIRECT3DDEVICE9));
			DEFINE_D3DX_FUNCTION(D3DXGetPixelShaderProfile,  LPCSTR, (LPDIRECT3DDEVICE9));
			DEFINE_D3DX_FUNCTION(D3DXSaveSurfaceToFileInMemory, HRESULT, (LPD3DXBUFFER*, D3DXIMAGE_FILEFORMAT, LPDIRECT3DSURFACE9, const PALETTEENTRY*, const RECT*));
			DEFINE_D3DX_FUNCTION(D3DXSaveSurfaceToFileA, HRESULT, (LPCSTR ,D3DXIMAGE_FILEFORMAT , LPDIRECT3DSURFACE9 , CONST PALETTEENTRY* , CONST RECT* ));
			DEFINE_D3DX_FUNCTION(D3DXGetShaderConstantTable, HRESULT, (const DWORD* , LPD3DXCONSTANTTABLE*));
			DEFINE_D3DX_FUNCTION(D3DXCreateBuffer, HRESULT, (DWORD, LPD3DXBUFFER*));

#undef DEFINE_D3DX_FUNCTION
#endif
		};

		class ShaderEnvironment
		{
		public:
			D3DMATRIX          modelMatrix;
			D3DMATRIX          viewMatrix;
			D3DMATRIX          projMatrix;
			D3DMATRIX          modelViewMatrix;
			D3DMATRIX          modelViewProjMatrix;

			D3DXMATRIX         boneMatrices[RENDERER_MAX_BONES];
			PxU32              numBones;

			float              fogColorAndDistance[4];

			float              eyePosition[3];
			float              eyeDirection[3];

			D3DCOLOR           ambientColor;

			D3DCOLOR           lightColor;
			float              lightIntensity;
			float              lightDirection[3];
			float              lightPosition[3];
			float              lightInnerRadius;
			float              lightOuterRadius;
			float              lightInnerCone;
			float              lightOuterCone;
			IDirect3DTexture9 *lightShadowMap;
			D3DXMATRIX         lightShadowMatrix;

		public:
			ShaderEnvironment(void);
		};

	protected:
		D3D9Renderer(IDirect3D9* inDirect3d, const char* devName, PxU32 dispWidth, PxU32 dispHeight, IDirect3DDevice9* inDevice, bool isDeviceEx, const char* assetDir);
		void initialize(bool isDeviceEx);

	public:
		D3D9Renderer(const RendererDesc &desc, const char* assetDir);
		virtual ~D3D9Renderer(void);

		IDirect3DDevice9        *getD3DDevice(void)               { return m_d3dDevice; }
		D3DXInterface           &getD3DX(void)                    { return m_d3dx; }
		ShaderEnvironment       &getShaderEnvironment(void)       { return m_environment; }
		const ShaderEnvironment &getShaderEnvironment(void) const { return m_environment; }

	protected:
		virtual bool checkResize(bool isDeviceLost);
		void buildDepthStencilSurface(void);
		void buildSwapchain();
		void releaseSwapchain();
		bool useSwapchain() const;
		bool validSwapchain() const;
		HRESULT presentSwapchain();

		void releaseDepthStencilSurface(void);
	public:
		bool resetDevice(void);
		void onDeviceLost(void);
		void onDeviceReset(void);

	public:
		// clears the offscreen buffers.
		virtual void clearBuffers(void);

		// presents the current color buffer to the screen.
		// returns true on device reset and if buffers need to be rewritten.
		virtual bool swapBuffers(void);

		// get the device pointer (void * abstraction)
		virtual void *getDevice()                                 { return static_cast<void*>(getD3DDevice()); }

		// get the window size
		void getWindowSize(PxU32 &width, PxU32 &height) const;

		// gets a handle to the current frame's data, in bitmap format
		//    note: subsequent calls will invalidate any previously returned data
		//    return true on successful screenshot capture
		virtual bool					captureScreen(PxU32 &width, PxU32& height, PxU32& sizeInBytes, const void*& screenshotData);

		virtual RendererVertexBuffer   *createVertexBuffer(  const RendererVertexBufferDesc   &desc);
		virtual RendererIndexBuffer    *createIndexBuffer(   const RendererIndexBufferDesc    &desc);
		virtual RendererInstanceBuffer *createInstanceBuffer(const RendererInstanceBufferDesc &desc);
		virtual RendererTexture2D      *createTexture2D(     const RendererTexture2DDesc      &desc);
		virtual RendererTexture3D      *createTexture3D(     const RendererTexture3DDesc      &desc);
		virtual RendererTarget         *createTarget(        const RendererTargetDesc         &desc);
		virtual RendererMaterial       *createMaterial(      const RendererMaterialDesc       &desc);
		virtual RendererMesh           *createMesh(          const RendererMeshDesc           &desc);
		virtual RendererLight          *createLight(         const RendererLightDesc          &desc);
		
		virtual void disableDepthTest();
		virtual void enableDepthTest();

		virtual void                    setVsync(bool on);

		virtual	bool					initTexter();
		virtual	void					closeTexter();

		virtual bool beginRender(void);
		virtual void endRender(void);

		bool canUseManagedResources() { return !m_isDeviceEx; }

		virtual void bindViewProj(const physx::PxMat44 &eye, const RendererProjection &proj);
		virtual void bindFogState(const RendererColor &fogColor, float fogDistance);
		virtual void bindAmbientState(const RendererColor &ambientColor);
		virtual void bindDeferredState(void);
		virtual void bindMeshContext(const RendererMeshContext &context);
		virtual void beginMultiPass(void);
		virtual void endMultiPass(void);
		virtual void beginTransparentMultiPass(void);
		virtual void endTransparentMultiPass(void);
		virtual void renderDeferredLight(const RendererLight &light);
		virtual PxU32 convertColor(const RendererColor& color) const;

		virtual bool isOk(void) const;

		virtual	void setupTextRenderStates();
		virtual	void resetTextRenderStates();
		virtual	void renderTextBuffer(const void* vertices, PxU32 nbVerts, const PxU16* indices, PxU32 nbIndices, RendererMaterial* material);
		virtual	void renderLines2D(const void* vertices, PxU32 nbVerts);
		virtual	void setupScreenquadRenderStates();
		virtual	void resetScreenquadRenderStates();

	private:
		void addResource(D3D9RendererResource &resource);
		void removeResource(D3D9RendererResource &resource);
		void notifyResourcesLostDevice(void);
		void notifyResourcesResetDevice(void);

	protected:
		PxU32                          m_displayWidth;
		PxU32                          m_displayHeight;
		ID3DXBuffer                   *m_displayBuffer;

		IDirect3D9                    *m_d3d;
		IDirect3DDevice9              *m_d3dDevice;
		IDirect3DSurface9             *m_d3dDepthStencilSurface;
		IDirect3DSurface9             *m_d3dSurface;

		IDirect3DSwapChain9           *m_d3dSwapChain;
		IDirect3DSurface9             *m_d3dSwapDepthStencilSurface;
		IDirect3DSurface9             *m_d3dSwapSurface;
	private:
		bool						   m_isDeviceEx; //implications all over the place

		D3DXInterface                  m_d3dx;

		ShaderEnvironment              m_environment;

		D3DPRESENT_PARAMETERS          m_d3dPresentParams;
		bool                           m_d3dPresentParamsChanged;

		physx::PxMat44				   m_viewMatrix;

		// non-managed resources...
		std::vector<D3D9RendererResource*> m_resources;

		IDirect3DVertexDeclaration9*	m_textVDecl;
	};

	class D3D9RendererResource
	{
		friend class D3D9Renderer;
	public:
		D3D9RendererResource(void)
		{
			m_d3dRenderer = 0;
		}

		virtual ~D3D9RendererResource(void)
		{
			if(m_d3dRenderer) m_d3dRenderer->removeResource(*this);
		}

	public:
		virtual void onDeviceLost(void)=0;
		virtual void onDeviceReset(void)=0;

	private:
		D3D9Renderer *m_d3dRenderer;
	};

} // namespace SampleRenderer

#endif // #if defined(RENDERER_ENABLE_DIRECT3D9)
#endif