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://www.ogre3d.org
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __D3D9RENDERSYSTEM_H__
00029 #define __D3D9RENDERSYSTEM_H__
00030 
00031 #include "OgreD3D9Prerequisites.h"
00032 #include "OgreString.h"
00033 #include "OgreStringConverter.h"
00034 #include "OgreRenderSystem.h"
00035 #include "OgreRenderSystemCapabilities.h"
00036 #include "OgreD3D9Mappings.h"
00037 
00038 namespace Ogre 
00039 {
00040 #define MAX_LIGHTS 8
00041 
00042     class D3D9DriverList;
00043     class D3D9Driver;
00044     class D3D9Device;
00045     class D3D9DeviceManager;
00046     class D3D9ResourceManager;
00047 
00051     class _OgreD3D9Export D3D9RenderSystem : public RenderSystem
00052     {
00053     public:
00054         enum MultiheadUseType
00055         {
00056             mutAuto,
00057             mutYes,
00058             mutNo
00059         };
00060         
00061     private:
00063         IDirect3D9*  mD3D;      
00064         // Stored options
00065         ConfigOptionMap mOptions;
00066         size_t mFSAASamples;
00067         String mFSAAHint;
00068 
00070         HINSTANCE mhInstance;
00071 
00073         D3D9DriverList* mDriverList;
00075         D3D9Driver* mActiveD3DDriver;
00077         bool mUseNVPerfHUD;
00079         bool mPerStageConstantSupport;
00081         static D3D9RenderSystem* msD3D9RenderSystem;
00082 
00084         struct sD3DTextureStageDesc
00085         {
00087             D3D9Mappings::eD3DTexType texType;
00089             size_t coordIndex;
00091             TexCoordCalcMethod autoTexCoordType;
00093             const Frustum *frustum;
00095             IDirect3DBaseTexture9 *pTex;
00097             IDirect3DBaseTexture9 *pVertexTex;
00098         } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
00099 
00100         // Array of up to 8 lights, indexed as per API
00101         // Note that a null value indicates a free slot
00102         Light* mLights[MAX_LIGHTS];     
00103         D3D9DriverList* getDirect3DDrivers();
00104         void refreshD3DSettings();
00105         void refreshFSAAOptions();
00106         
00107         void setD3D9Light( size_t index, Light* light );
00108         
00109         // state management methods, very primitive !!!
00110         HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
00111         HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
00112         HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
00113 
00114         HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
00115         {
00116 #if OGRE_DOUBLE_PRECISION == 1
00117             float temp = static_cast<float>(value);
00118             return __SetRenderState(state, *((LPDWORD)(&temp)));
00119 #else
00120             return __SetRenderState(state, *((LPDWORD)(&value)));
00121 #endif
00122         }
00123 
00125         DWORD _getCurrentAnisotropy(size_t unit);
00127         bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
00128         
00129         D3D9HardwareBufferManager* mHardwareBufferManager;
00130         D3D9GpuProgramManager* mGpuProgramManager;
00131         D3D9HLSLProgramFactory* mHLSLProgramFactory;
00132         D3D9ResourceManager* mResourceManager;
00133         D3D9DeviceManager* mDeviceManager;
00134 
00135         size_t mLastVertexSourceCount;
00136 
00137 
00139         virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
00140         RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
00141 
00143         virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
00144 
00145 
00146         void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
00147         void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
00148         bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
00149         
00150         HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
00152         Matrix4 mViewMatrix;
00153 
00154         D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
00155     
00156         typedef vector<D3D9RenderWindow*>::type D3D9RenderWindowList;
00157         // List of additional windows after the first (swap chains)
00158         D3D9RenderWindowList mRenderWindows;
00159         
00162         typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
00163         DepthStencilHash mDepthStencilHash;
00164 
00165         MultiheadUseType mMultiheadUse;
00166 
00167     protected:
00168         void setClipPlanesImpl(const PlaneList& clipPlanes);        
00169     public:
00170         // constructor
00171         D3D9RenderSystem( HINSTANCE hInstance );
00172         // destructor
00173         ~D3D9RenderSystem();
00174 
00175         virtual void initConfigOptions();
00176 
00177         // Overridden RenderSystem functions
00178         ConfigOptionMap& getConfigOptions();
00179         String validateConfigOptions();
00180         RenderWindow* _initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window"  );
00182         RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height, 
00183             bool fullScreen, const NameValuePairList *miscParams = 0);
00184         
00186         bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions, 
00187             RenderWindowList& createdWindows);
00188 
00190         DepthBuffer* _createDepthBufferFor( RenderTarget *renderTarget );
00191 
00197         DepthBuffer* _addManualDepthBuffer( IDirect3DDevice9* depthSurfaceDevice, IDirect3DSurface9 *surf );
00198 
00208         using RenderSystem::_cleanupDepthBuffers;
00209         void _cleanupDepthBuffers( IDirect3DDevice9 *creator );
00210 
00218         void _cleanupDepthBuffers( IDirect3DSurface9 *manualSurface );
00219 
00223         void _setRenderTarget(RenderTarget *target);
00224         
00226         virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
00227 
00228         String getErrorDescription( long errorNumber ) const;
00229         const String& getName() const;
00230         // Low-level overridden members
00231         void setConfigOption( const String &name, const String &value );
00232         void reinitialise();
00233         void shutdown();
00234         void setAmbientLight( float r, float g, float b );
00235         void setShadingType( ShadeOptions so );
00236         void setLightingEnabled( bool enabled );
00237         void destroyRenderTarget(const String& name);
00238         VertexElementType getColourVertexElementType() const;
00239         void setStencilCheckEnabled(bool enabled);
00240         void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS, 
00241             uint32 refValue = 0, uint32 mask = 0xFFFFFFFF, 
00242             StencilOperation stencilFailOp = SOP_KEEP, 
00243             StencilOperation depthFailOp = SOP_KEEP,
00244             StencilOperation passOp = SOP_KEEP, 
00245             bool twoSidedOperation = false);
00246         void setNormaliseNormals(bool normalise);
00247 
00248         // Low-level overridden members, mainly for internal use
00249         void _useLights(const LightList& lights, unsigned short limit);
00250         void _setWorldMatrix( const Matrix4 &m );
00251         void _setViewMatrix( const Matrix4 &m );
00252         void _setProjectionMatrix( const Matrix4 &m );
00253         void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
00254         void _setPointSpritesEnabled(bool enabled);
00255         void _setPointParameters(Real size, bool attenuationEnabled, 
00256             Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
00257         void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
00258         void _setVertexTexture(size_t unit, const TexturePtr& tex);
00259         void _disableTextureUnit(size_t texUnit);
00260         void _setTextureCoordSet( size_t unit, size_t index );
00261         void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m, 
00262             const Frustum* frustum = 0);
00263         void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
00264         void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
00265         void _setTextureBorderColour(size_t stage, const ColourValue& colour);
00266         void _setTextureMipmapBias(size_t unit, float bias);
00267         void _setTextureMatrix( size_t unit, const Matrix4 &xform );
00268         void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
00269         void _setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
00270         void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
00271         void _setViewport( Viewport *vp );      
00272         void _beginFrame();
00273         virtual RenderSystemContext* _pauseFrame(void);
00274         virtual void _resumeFrame(RenderSystemContext* context);
00275         void _endFrame();       
00276         void _setCullingMode( CullingMode mode );
00277         void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
00278         void _setDepthBufferCheckEnabled( bool enabled = true );
00279         void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
00280         void _setDepthBufferWriteEnabled(bool enabled = true);
00281         void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
00282         void _setDepthBias(float constantBias, float slopeScaleBias);
00283         void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
00284         void _convertProjectionMatrix(const Matrix4& matrix,
00285             Matrix4& dest, bool forGpuProgram = false);
00286         void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00287             Matrix4& dest, bool forGpuProgram = false);
00288         void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane, 
00289             Real farPlane, Matrix4& dest, bool forGpuProgram = false);
00290         void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00291             Matrix4& dest, bool forGpuProgram = false);
00292         void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane, 
00293             bool forGpuProgram);
00294         void _setPolygonMode(PolygonMode level);
00295         void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
00296         void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
00297         void setVertexDeclaration(VertexDeclaration* decl);
00298         void setVertexDeclaration(VertexDeclaration* decl, bool useGlobalInstancingVertexBufferIsAvailable);
00299         void setVertexBufferBinding(VertexBufferBinding* binding);
00300         void setVertexBufferBinding(VertexBufferBinding* binding, size_t numberOfInstances, bool useGlobalInstancingVertexBufferIsAvailable, bool indexesUsed);
00301         void _render(const RenderOperation& op);
00305         void bindGpuProgram(GpuProgram* prg);
00309         void unbindGpuProgram(GpuProgramType gptype);
00313         void bindGpuProgramParameters(GpuProgramType gptype, 
00314             GpuProgramParametersSharedPtr params, uint16 variabilityMask);
00315         void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
00316 
00317         void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
00318         void clearFrameBuffer(unsigned int buffers, 
00319             const ColourValue& colour = ColourValue::Black, 
00320             Real depth = 1.0f, unsigned short stencil = 0);
00321         void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
00322         void enableClipPlane (ushort index, bool enable);
00323         HardwareOcclusionQuery* createHardwareOcclusionQuery();
00324         Real getHorizontalTexelOffset();
00325         Real getVerticalTexelOffset();
00326         Real getMinimumDepthInputValue();
00327         Real getMaximumDepthInputValue();
00328         void registerThread();
00329         void unregisterThread();
00330         void preExtraThreadsStarted();
00331         void postExtraThreadsStarted();     
00332         
00333         static D3D9ResourceManager* getResourceManager();
00334         static D3D9DeviceManager* getDeviceManager();
00335         static IDirect3D9* getDirect3D9();
00336         static UINT getResourceCreationDeviceCount();
00337         static IDirect3DDevice9* getResourceCreationDevice(UINT index);
00338         static IDirect3DDevice9* getActiveD3D9Device();
00339 
00343         D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
00344 
00348         bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
00349 
00351         void determineFSAASettings(IDirect3DDevice9* d3d9Device, size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat, 
00352             bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
00353 
00355         unsigned int getDisplayMonitorCount() const;
00356         
00358         void fireDeviceEvent( D3D9Device* device, const String & name );
00359 
00361         MultiheadUseType getMultiheadUse() const { return mMultiheadUse; }
00362     protected:  
00364         DWORD getSamplerId(size_t unit);
00365 
00367         void notifyOnDeviceLost(D3D9Device* device);
00368 
00370         void notifyOnDeviceReset(D3D9Device* device);
00371 
00372     private:
00373         friend class D3D9Device;
00374         friend class D3D9DeviceManager;     
00375     };
00376 }
00377 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 2012 21:48:49