Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OgreD3D9RenderSystem.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://ogre.sourceforge.net/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __D3D9RENDERSYSTEM_H__
00026 #define __D3D9RENDERSYSTEM_H__
00027 
00028 #include "OgreD3D9Prerequisites.h"
00029 #include "OgreString.h"
00030 #include "OgreStringConverter.h"
00031 #include "OgreRenderSystem.h"
00032 #include "OgreD3D9Mappings.h"
00033 
00034 #include "OgreNoMemoryMacros.h"
00035 #include <d3d9.h>
00036 #include <d3dx9.h>
00037 #include <dxerr9.h>
00038 #include "OgreMemoryMacros.h"
00039 
00040 namespace Ogre 
00041 {
00042 #define MAX_LIGHTS 8
00043 
00044     class D3D9DriverList;
00045     class D3D9Driver;
00046 
00050     class D3D9RenderSystem : public RenderSystem
00051     {
00052     private:
00054         LPDIRECT3D9         mpD3D;
00056         LPDIRECT3DDEVICE9   mpD3DDevice;
00057         
00058         // Stored options
00059         ConfigOptionMap mOptions;
00061         D3DMULTISAMPLE_TYPE mFSAAType;
00063         DWORD mFSAAQuality;
00064 
00066         HINSTANCE mhInstance;
00067 
00069         D3D9DriverList* mDriverList;
00071         D3D9Driver* mActiveD3DDriver;
00073         D3DCAPS9 mCaps;
00075         bool mUseNVPerfHUD;
00077         bool mPerStageConstantSupport;
00078 
00080         struct sD3DTextureStageDesc
00081         {
00083             D3D9Mappings::eD3DTexType texType;
00085             size_t coordIndex;
00087             TexCoordCalcMethod autoTexCoordType;
00089             const Frustum *frustum;
00091             IDirect3DBaseTexture9 *pTex;
00092         } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
00093 
00094         // Array of up to 8 lights, indexed as per API
00095         // Note that a null value indeicates a free slot
00096         Light* mLights[MAX_LIGHTS];
00097 
00098         D3D9DriverList* getDirect3DDrivers(void);
00099         void refreshD3DSettings(void);
00100         void refreshFSAAOptions(void);
00101         void freeDevice(void);
00102 
00103         inline bool compareDecls( D3DVERTEXELEMENT9* pDecl1, D3DVERTEXELEMENT9* pDecl2, size_t size );
00104 
00105 
00106         void initInputDevices(void);
00107         void processInputDevices(void);
00108         void setD3D9Light( size_t index, Light* light );
00109         
00110         // state management methods, very primitive !!!
00111         HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
00112         HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
00113         HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
00114 
00115         HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
00116         {
00117 #if OGRE_DOUBLE_PRECISION == 1
00118             float temp = static_cast<float>(value);
00119             return __SetRenderState(state, *((LPDWORD)(&temp)));
00120 #else
00121             return __SetRenderState(state, *((LPDWORD)(&value)));
00122 #endif
00123         }
00124 
00126         DWORD _getCurrentAnisotropy(size_t unit);
00128         bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
00130         void _setFSAA(D3DMULTISAMPLE_TYPE type, DWORD qualityLevel);
00131         
00132         D3D9HardwareBufferManager* mHardwareBufferManager;
00133         D3D9GpuProgramManager* mGpuProgramManager;
00134         //D3D9HLSLProgramFactory* mHLSLProgramFactory;
00135 
00136         size_t mLastVertexSourceCount;
00137 
00138 
00140         void initCapabilities(void);
00141 
00142         void convertVertexShaderCaps(void);
00143         void convertPixelShaderCaps(void);
00144 
00145         unsigned short mCurrentLights;
00147         Matrix4 mViewMatrix;
00148 
00149         // What follows is a set of duplicated lists just to make it
00150         // easier to deal with lost devices
00151         
00153         D3D9RenderWindow* mPrimaryWindow;
00154 
00155         typedef std::vector<D3D9RenderWindow*> SecondaryWindowList;
00156         // List of additional windows after the first (swap chains)
00157         SecondaryWindowList mSecondaryWindows;
00158 
00159         bool mDeviceLost;
00160         bool mBasicStatesInitialised;
00161 
00164         typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
00165         DepthStencilHash mDepthStencilHash;
00166 
00172         typedef std::pair<D3DFORMAT, D3DMULTISAMPLE_TYPE> ZBufferFormat;
00173         struct ZBufferRef
00174         {
00175             IDirect3DSurface9 *surface;
00176             size_t width, height;
00177         };
00178         typedef std::map<ZBufferFormat, ZBufferRef> ZBufferHash;
00179         ZBufferHash mZBufferHash;
00180     public:
00181         // constructor
00182         D3D9RenderSystem( HINSTANCE hInstance );
00183         // destructor
00184         ~D3D9RenderSystem();
00185 
00186         virtual void initConfigOptions(void);
00187 
00188         // Overridden RenderSystem functions
00189         ConfigOptionMap& getConfigOptions(void);
00190         String validateConfigOptions(void);
00191         RenderWindow* initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window"  );
00193         RenderWindow* createRenderWindow(const String &name, unsigned int width, unsigned int height, 
00194             bool fullScreen, const NameValuePairList *miscParams = 0);
00195 
00197         RenderTexture * createRenderTexture( const String & name, unsigned int width, unsigned int height,
00198             TextureType texType = TEX_TYPE_2D, PixelFormat internalFormat = PF_X8R8G8B8, 
00199             const NameValuePairList *miscParams = 0 ); 
00200 
00202         virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
00203 
00204         String getErrorDescription( long errorNumber ) const;
00205         const String& getName(void) const;
00206         // Low-level overridden members
00207         void setConfigOption( const String &name, const String &value );
00208         void reinitialise();
00209         void shutdown();
00210         void setAmbientLight( float r, float g, float b );
00211         void setShadingType( ShadeOptions so );
00212         void setLightingEnabled( bool enabled );
00213         void destroyRenderTarget(const String& name);
00214         VertexElementType getColourVertexElementType(void) const;
00215         void setStencilCheckEnabled(bool enabled);
00216         void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS, 
00217             uint32 refValue = 0, uint32 mask = 0xFFFFFFFF, 
00218             StencilOperation stencilFailOp = SOP_KEEP, 
00219             StencilOperation depthFailOp = SOP_KEEP,
00220             StencilOperation passOp = SOP_KEEP, 
00221             bool twoSidedOperation = false);
00222         void setNormaliseNormals(bool normalise);
00223 
00224         // Low-level overridden members, mainly for internal use
00225         void _useLights(const LightList& lights, unsigned short limit);
00226         void _setWorldMatrix( const Matrix4 &m );
00227         void _setViewMatrix( const Matrix4 &m );
00228         void _setProjectionMatrix( const Matrix4 &m );
00229         void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
00230         void _setPointSpritesEnabled(bool enabled);
00231         void _setPointParameters(Real size, bool attenuationEnabled, 
00232             Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
00233         void _setTexture( size_t unit, bool enabled, const String &texname );
00234         void _setTextureCoordSet( size_t unit, size_t index );
00235         void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m, 
00236             const Frustum* frustum = 0);
00237         void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
00238         void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
00239         void _setTextureBorderColour(size_t stage, const ColourValue& colour);
00240         void _setTextureMatrix( size_t unit, const Matrix4 &xform );
00241         void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor );
00242         void _setAlphaRejectSettings( CompareFunction func, unsigned char value );
00243         void _setViewport( Viewport *vp );
00244         void _beginFrame(void);
00245         void _endFrame(void);
00246         void _setCullingMode( CullingMode mode );
00247         void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
00248         void _setDepthBufferCheckEnabled( bool enabled = true );
00249         void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
00250         void _setDepthBufferWriteEnabled(bool enabled = true);
00251         void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
00252         void _setDepthBias(ushort bias);
00253         void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
00254         void _convertProjectionMatrix(const Matrix4& matrix,
00255             Matrix4& dest, bool forGpuProgram = false);
00256         void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00257             Matrix4& dest, bool forGpuProgram = false);
00258         void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane, 
00259             Real farPlane, Matrix4& dest, bool forGpuProgram = false);
00260         void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00261             Matrix4& dest, bool forGpuProgram = false);
00262         void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane, 
00263             bool forGpuProgram);
00264         void _setPolygonMode(PolygonMode level);
00265         void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
00266         void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
00267         void setVertexDeclaration(VertexDeclaration* decl);
00268         void setVertexBufferBinding(VertexBufferBinding* binding);
00269         void _render(const RenderOperation& op);
00273         void bindGpuProgram(GpuProgram* prg);
00277         void unbindGpuProgram(GpuProgramType gptype);
00281         void bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params);
00285         void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
00289         void setClipPlanes(const PlaneList& clipPlanes);
00290 
00291         void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
00292         void clearFrameBuffer(unsigned int buffers, 
00293             const ColourValue& colour = ColourValue::Black, 
00294             Real depth = 1.0f, unsigned short stencil = 0);
00295         void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
00296         void enableClipPlane (ushort index, bool enable);
00297         HardwareOcclusionQuery* createHardwareOcclusionQuery(void);
00298         Real getHorizontalTexelOffset(void);
00299         Real getVerticalTexelOffset(void);
00300         Real getMinimumDepthInputValue(void);
00301         Real getMaximumDepthInputValue(void);
00302 
00304         void restoreLostDevice(void);
00306         bool isDeviceLost(void);
00308         void _notifyDeviceLost(void);
00309 
00313         D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
00314 
00319         IDirect3DSurface9* _getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, size_t width, size_t height);
00320 
00323         void _cleanupDepthStencils();
00324     };
00325 }
00326 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jan 21 10:01:35 2007