source: OGRE/trunk/ogre_changes/Ogre1.2/RenderSystems/Direct3D9/include/OgreD3D9RenderSystem.h @ 1053

Revision 1053, 13.5 KB checked in by szirmay, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __D3D9RENDERSYSTEM_H__
26#define __D3D9RENDERSYSTEM_H__
27
28#include "OgreD3D9Prerequisites.h"
29#include "OgreString.h"
30#include "OgreStringConverter.h"
31#include "OgreRenderSystem.h"
32#include "OgreD3D9Mappings.h"
33
34#include "OgreNoMemoryMacros.h"
35#include <d3d9.h>
36#include <d3dx9.h>
37#include <dxerr9.h>
38#include "OgreMemoryMacros.h"
39
40namespace Ogre
41{
42#define MAX_LIGHTS 8
43
44        class D3D9DriverList;
45        class D3D9Driver;
46
47        /**
48        Implementation of DirectX9 as a rendering system.
49        */
50        class D3D9RenderSystem : public RenderSystem
51        {
52        private:
53                /// Direct3D
54                LPDIRECT3D9                     mpD3D;
55                /// Direct3D rendering device
56                LPDIRECT3DDEVICE9       mpD3DDevice;
57               
58                // Stored options
59                ConfigOptionMap mOptions;
60                /// full-screen multisampling antialiasing type
61                D3DMULTISAMPLE_TYPE mFSAAType;
62                /// full-screen multisampling antialiasing level
63                DWORD mFSAAQuality;
64
65                /// instance
66                HINSTANCE mhInstance;
67
68                /// List of D3D drivers installed (video cards)
69                D3D9DriverList* mDriverList;
70                /// Currently active driver
71                D3D9Driver* mActiveD3DDriver;
72                /// Device caps.
73                D3DCAPS9 mCaps;
74                /// NVPerfHUD allowed?
75                bool mUseNVPerfHUD;
76
77                /// structure holding texture unit settings for every stage
78                struct sD3DTextureStageDesc
79                {
80                        /// the type of the texture
81                        D3D9Mappings::eD3DTexType texType;
82                        /// wich texCoordIndex to use
83                        size_t coordIndex;
84                        /// type of auto tex. calc. used
85                        TexCoordCalcMethod autoTexCoordType;
86            /// Frustum, used if the above is projection
87            const Frustum *frustum;
88                        /// texture
89                        IDirect3DBaseTexture9 *pTex;
90                }
91#ifdef GAMETOOLS_ILLUMINATION_MODULE
92                mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS + 4];
93#else
94                mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
95#endif
96
97                // Array of up to 8 lights, indexed as per API
98                // Note that a null value indeicates a free slot
99                Light* mLights[MAX_LIGHTS];
100
101                D3D9DriverList* getDirect3DDrivers(void);
102                void refreshD3DSettings(void);
103                void freeDevice(void);
104
105                inline bool compareDecls( D3DVERTEXELEMENT9* pDecl1, D3DVERTEXELEMENT9* pDecl2, size_t size );
106
107
108                void initInputDevices(void);
109                void processInputDevices(void);
110                void setD3D9Light( size_t index, Light* light );
111               
112                // state management methods, very primitive !!!
113                HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
114                HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
115                HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
116
117                HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
118                {
119#if OGRE_DOUBLE_PRECISION == 1
120                        float temp = static_cast<float>(value);
121                        return __SetRenderState(state, *((LPDWORD)(&temp)));
122#else
123                        return __SetRenderState(state, *((LPDWORD)(&value)));
124#endif
125                }
126
127                /// return anisotropy level
128                DWORD _getCurrentAnisotropy(size_t unit);
129                /// check if a FSAA is supported
130                bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
131                /// set FSAA
132                void _setFSAA(D3DMULTISAMPLE_TYPE type, DWORD qualityLevel);
133               
134                D3D9HardwareBufferManager* mHardwareBufferManager;
135                D3D9GpuProgramManager* mGpuProgramManager;
136        //D3D9HLSLProgramFactory* mHLSLProgramFactory;
137
138                size_t mLastVertexSourceCount;
139
140
141        /// Internal method for populating the capabilities structure
142        void initCapabilities(void);
143
144        void convertVertexShaderCaps(void);
145        void convertPixelShaderCaps(void);
146
147        unsigned short mCurrentLights;
148        /// Saved last view matrix
149        Matrix4 mViewMatrix;
150
151                // What follows is a set of duplicated lists just to make it
152                // easier to deal with lost devices
153               
154                /// Primary window, the one used to create the device
155                D3D9RenderWindow* mPrimaryWindow;
156
157                typedef std::vector<D3D9RenderWindow*> SecondaryWindowList;
158                // List of additional windows after the first (swap chains)
159                SecondaryWindowList mSecondaryWindows;
160
161                bool mDeviceLost;
162                bool mBasicStatesInitialised;
163
164                /** Mapping of texture format -> DepthStencil. Used as cache by _getDepthStencilFormatFor
165                */
166                typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
167                DepthStencilHash mDepthStencilHash;
168
169                /** Mapping of depthstencil format -> depthstencil buffer
170                        Keep one depthstencil buffer around for every format that is used, it must be large
171                        enough to hold the largest rendering target.
172                        This is used as cache by _getDepthStencilFor.
173                */
174                typedef std::pair<D3DFORMAT, D3DMULTISAMPLE_TYPE> ZBufferFormat;
175                struct ZBufferRef
176                {
177                        IDirect3DSurface9 *surface;
178                        size_t width, height;
179                };
180                typedef std::map<ZBufferFormat, ZBufferRef> ZBufferHash;
181                ZBufferHash mZBufferHash;
182        public:
183                // constructor
184                D3D9RenderSystem( HINSTANCE hInstance );
185                // destructor
186                ~D3D9RenderSystem();
187
188                virtual void initConfigOptions(void);
189
190                // Overridden RenderSystem functions
191                ConfigOptionMap& getConfigOptions(void);
192                String validateConfigOptions(void);
193                RenderWindow* initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window"  );
194                /// @copydoc RenderSystem::createRenderWindow
195                RenderWindow* createRenderWindow(const String &name, unsigned int width, unsigned int height,
196                        bool fullScreen, const NameValuePairList *miscParams = 0);
197
198                /// @copydoc RenderSystem::createRenderTexture
199                RenderTexture * createRenderTexture( const String & name, unsigned int width, unsigned int height,
200                        TextureType texType = TEX_TYPE_2D, PixelFormat internalFormat = PF_X8R8G8B8,
201                        const NameValuePairList *miscParams = 0 );
202
203                /// @copydoc RenderSystem::createMultiRenderTarget
204                virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
205
206                String getErrorDescription( long errorNumber ) const;
207                const String& getName(void) const;
208                // Low-level overridden members
209                void setConfigOption( const String &name, const String &value );
210                void reinitialise();
211                void shutdown();
212                void setAmbientLight( float r, float g, float b );
213                void setShadingType( ShadeOptions so );
214                void setLightingEnabled( bool enabled );
215                void destroyRenderTarget(const String& name);
216                VertexElementType getColourVertexElementType(void) const;
217                void setStencilCheckEnabled(bool enabled);
218        void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
219            uint32 refValue = 0, uint32 mask = 0xFFFFFFFF,
220            StencilOperation stencilFailOp = SOP_KEEP,
221            StencilOperation depthFailOp = SOP_KEEP,
222            StencilOperation passOp = SOP_KEEP,
223            bool twoSidedOperation = false);
224        void setNormaliseNormals(bool normalise);
225
226                // Low-level overridden members, mainly for internal use
227        void _useLights(const LightList& lights, unsigned short limit);
228                void _setWorldMatrix( const Matrix4 &m );
229                void _setViewMatrix( const Matrix4 &m );
230                void _setProjectionMatrix( const Matrix4 &m );
231                void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
232                void _setPointSpritesEnabled(bool enabled);
233                void _setPointParameters(Real size, bool attenuationEnabled,
234                        Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
235                void _setTexture( size_t unit, bool enabled, const String &texname );
236        void _setTextureCoordSet( size_t unit, size_t index );
237        void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m,
238            const Frustum* frustum = 0);
239                void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
240        void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
241        void _setTextureBorderColour(size_t stage, const ColourValue& colour);
242                void _setTextureMatrix( size_t unit, const Matrix4 &xform );
243#ifdef GAMETOOLS_ILLUMINATION_MODULE
244                virtual void _setSceneBlending(SceneBlendFactor sourceFactor,
245                                                                                SceneBlendFactor destFactor,
246                                                                                SceneBlendOperation blendOp = SBOP_ADD,
247                                                                                bool separateAlpha = false,
248                                                                                SceneBlendFactor sourceFactorAlpha = SBF_ONE,
249                                                                                SceneBlendFactor destFactorAlpha = SBF_ZERO,
250                                                                                SceneBlendOperation blendOpAlpha = SBOP_ADD);
251#else
252                void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor );
253#endif
254                void _setAlphaRejectSettings( CompareFunction func, unsigned char value );
255                void _setViewport( Viewport *vp );
256                void _beginFrame(void);
257                void _endFrame(void);
258                void _setCullingMode( CullingMode mode );
259                void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
260                void _setDepthBufferCheckEnabled( bool enabled = true );
261                void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
262                void _setDepthBufferWriteEnabled(bool enabled = true);
263                void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
264                void _setDepthBias(ushort bias);
265                void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
266                void _convertProjectionMatrix(const Matrix4& matrix,
267            Matrix4& dest, bool forGpuProgram = false);
268                void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
269            Matrix4& dest, bool forGpuProgram = false);
270                void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane,
271            Real farPlane, Matrix4& dest, bool forGpuProgram = false);
272                void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
273            Matrix4& dest, bool forGpuProgram = false);
274        void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
275            bool forGpuProgram);
276                void _setPolygonMode(PolygonMode level);
277        void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
278                void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
279                void setVertexDeclaration(VertexDeclaration* decl);
280                void setVertexBufferBinding(VertexBufferBinding* binding);
281        void _render(const RenderOperation& op);
282        /** See
283          RenderSystem
284         */
285        void bindGpuProgram(GpuProgram* prg);
286        /** See
287          RenderSystem
288         */
289        void unbindGpuProgram(GpuProgramType gptype);
290        /** See
291          RenderSystem
292         */
293        void bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params);
294        /** See
295          RenderSystem
296         */
297        void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
298        /** See
299          RenderSystem
300         */
301        void setClipPlanes(const PlaneList& clipPlanes);
302
303        void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
304        void clearFrameBuffer(unsigned int buffers,
305            const ColourValue& colour = ColourValue::Black,
306            Real depth = 1.0f, unsigned short stencil = 0);
307                void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
308                void enableClipPlane (ushort index, bool enable);
309        HardwareOcclusionQuery* createHardwareOcclusionQuery(void);
310        Real getHorizontalTexelOffset(void);
311        Real getVerticalTexelOffset(void);
312        Real getMinimumDepthInputValue(void);
313        Real getMaximumDepthInputValue(void);
314
315                /** D3D specific method to restore a lost device. */
316                void restoreLostDevice(void);
317                /** D3D specific method to return whether the device has been lost. */
318                bool isDeviceLost(void);
319                /** Notify that a device has been lost */
320                void _notifyDeviceLost(void);
321
322                /** Check which depthStencil formats can be used with a certain pixel format,
323                        and return the best suited.
324                */
325                D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
326
327                /** Get a depth stencil surface that is compatible with an internal pixel format and
328                        multisample type.
329                        @returns A directx surface, or 0 if there is no compatible depthstencil possible.
330                */
331                IDirect3DSurface9* _getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, size_t width, size_t height);
332
333                /** Clear all cached depth stencil surfaces
334                */
335                void _cleanupDepthStencils();
336
337#ifdef GAMETOOLS_ILLUMINATION_MODULE
338                virtual void _setTextureUnitSettings(size_t texUnit, TextureUnitState& tl);
339#endif
340        };
341}
342#endif
Note: See TracBrowser for help on using the repository browser.