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         void detachRenderTargetImpl(const String& name);
00150         
00151         HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
00153         Matrix4 mViewMatrix;
00154 
00155         D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
00156     
00157         typedef vector<D3D9RenderWindow*>::type D3D9RenderWindowList;
00158         // List of additional windows after the first (swap chains)
00159         D3D9RenderWindowList mRenderWindows;
00160         
00163         typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
00164         DepthStencilHash mDepthStencilHash;
00165 
00166         MultiheadUseType mMultiheadUse;
00167 
00168     protected:
00169         void setClipPlanesImpl(const PlaneList& clipPlanes);        
00170     public:
00171         // constructor
00172         D3D9RenderSystem( HINSTANCE hInstance );
00173         // destructor
00174         ~D3D9RenderSystem();
00175 
00176         virtual void initConfigOptions();
00177 
00178         // Overridden RenderSystem functions
00179         ConfigOptionMap& getConfigOptions();
00180         String validateConfigOptions();
00181         RenderWindow* _initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window"  );
00183         RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height, 
00184             bool fullScreen, const NameValuePairList *miscParams = 0);
00185         
00187         bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions, 
00188             RenderWindowList& createdWindows);
00189 
00191         DepthBuffer* _createDepthBufferFor( RenderTarget *renderTarget );
00192 
00198         DepthBuffer* _addManualDepthBuffer( IDirect3DDevice9* depthSurfaceDevice, IDirect3DSurface9 *surf );
00199 
00209         using RenderSystem::_cleanupDepthBuffers;
00210         void _cleanupDepthBuffers( IDirect3DDevice9 *creator );
00211 
00219         void _cleanupDepthBuffers( IDirect3DSurface9 *manualSurface );
00220 
00224         void _setRenderTarget(RenderTarget *target);
00225         
00227         virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
00228 
00230         virtual RenderTarget * detachRenderTarget(const String &name);
00231 
00232         String getErrorDescription( long errorNumber ) const;
00233         const String& getName() const;
00234         // Low-level overridden members
00235         void setConfigOption( const String &name, const String &value );
00236         void reinitialise();
00237         void shutdown();
00238         void setAmbientLight( float r, float g, float b );
00239         void setShadingType( ShadeOptions so );
00240         void setLightingEnabled( bool enabled );
00241         void destroyRenderTarget(const String& name);
00242         VertexElementType getColourVertexElementType() const;
00243         void setStencilCheckEnabled(bool enabled);
00244         void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS, 
00245             uint32 refValue = 0, uint32 mask = 0xFFFFFFFF, 
00246             StencilOperation stencilFailOp = SOP_KEEP, 
00247             StencilOperation depthFailOp = SOP_KEEP,
00248             StencilOperation passOp = SOP_KEEP, 
00249             bool twoSidedOperation = false);
00250         void setNormaliseNormals(bool normalise);
00251 
00252         // Low-level overridden members, mainly for internal use
00253         void _useLights(const LightList& lights, unsigned short limit);
00254         void _setWorldMatrix( const Matrix4 &m );
00255         void _setViewMatrix( const Matrix4 &m );
00256         void _setProjectionMatrix( const Matrix4 &m );
00257         void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
00258         void _setPointSpritesEnabled(bool enabled);
00259         void _setPointParameters(Real size, bool attenuationEnabled, 
00260             Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
00261         void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
00262         void _setVertexTexture(size_t unit, const TexturePtr& tex);
00263         void _disableTextureUnit(size_t texUnit);
00264         void _setTextureCoordSet( size_t unit, size_t index );
00265         void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m, 
00266             const Frustum* frustum = 0);
00267         void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
00268         void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
00269         void _setTextureBorderColour(size_t stage, const ColourValue& colour);
00270         void _setTextureMipmapBias(size_t unit, float bias);
00271         void _setTextureMatrix( size_t unit, const Matrix4 &xform );
00272         void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
00273         void _setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
00274         void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
00275         void _setViewport( Viewport *vp );      
00276         void _beginFrame();
00277         virtual RenderSystemContext* _pauseFrame(void);
00278         virtual void _resumeFrame(RenderSystemContext* context);
00279         void _endFrame();       
00280         void _setCullingMode( CullingMode mode );
00281         void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
00282         void _setDepthBufferCheckEnabled( bool enabled = true );
00283         void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
00284         void _setDepthBufferWriteEnabled(bool enabled = true);
00285         void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
00286         void _setDepthBias(float constantBias, float slopeScaleBias);
00287         void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
00288         void _convertProjectionMatrix(const Matrix4& matrix,
00289             Matrix4& dest, bool forGpuProgram = false);
00290         void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00291             Matrix4& dest, bool forGpuProgram = false);
00292         void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane, 
00293             Real farPlane, Matrix4& dest, bool forGpuProgram = false);
00294         void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00295             Matrix4& dest, bool forGpuProgram = false);
00296         void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane, 
00297             bool forGpuProgram);
00298         void _setPolygonMode(PolygonMode level);
00299         void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
00300         void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
00301         void setVertexDeclaration(VertexDeclaration* decl);
00302         void setVertexDeclaration(VertexDeclaration* decl, bool useGlobalInstancingVertexBufferIsAvailable);
00303         void setVertexBufferBinding(VertexBufferBinding* binding);
00304         void setVertexBufferBinding(VertexBufferBinding* binding, size_t numberOfInstances, bool useGlobalInstancingVertexBufferIsAvailable, bool indexesUsed);
00305         void _render(const RenderOperation& op);
00309         void bindGpuProgram(GpuProgram* prg);
00313         void unbindGpuProgram(GpuProgramType gptype);
00317         void bindGpuProgramParameters(GpuProgramType gptype, 
00318             GpuProgramParametersSharedPtr params, uint16 variabilityMask);
00319         void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
00320 
00321         void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
00322         void clearFrameBuffer(unsigned int buffers, 
00323             const ColourValue& colour = ColourValue::Black, 
00324             Real depth = 1.0f, unsigned short stencil = 0);
00325         void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
00326         void enableClipPlane (ushort index, bool enable);
00327         HardwareOcclusionQuery* createHardwareOcclusionQuery();
00328         Real getHorizontalTexelOffset();
00329         Real getVerticalTexelOffset();
00330         Real getMinimumDepthInputValue();
00331         Real getMaximumDepthInputValue();
00332         void registerThread();
00333         void unregisterThread();
00334         void preExtraThreadsStarted();
00335         void postExtraThreadsStarted();     
00336         
00337         static D3D9ResourceManager* getResourceManager();
00338         static D3D9DeviceManager* getDeviceManager();
00339         static IDirect3D9* getDirect3D9();
00340         static UINT getResourceCreationDeviceCount();
00341         static IDirect3DDevice9* getResourceCreationDevice(UINT index);
00342         static IDirect3DDevice9* getActiveD3D9Device();
00343 
00347         D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
00348 
00352         bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
00353 
00355         void determineFSAASettings(IDirect3DDevice9* d3d9Device, size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat, 
00356             bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
00357 
00359         unsigned int getDisplayMonitorCount() const;
00360 
00362         virtual void beginProfileEvent( const String &eventName );
00363 
00365         virtual void endProfileEvent( void );
00366 
00368         virtual void markProfileEvent( const String &eventName );
00369         
00371         void fireDeviceEvent( D3D9Device* device, const String & name );
00372 
00374         MultiheadUseType getMultiheadUse() const { return mMultiheadUse; }
00375     protected:  
00377         DWORD getSamplerId(size_t unit);
00378 
00380         void notifyOnDeviceLost(D3D9Device* device);
00381 
00383         void notifyOnDeviceReset(D3D9Device* device);
00384 
00385     private:
00386         friend class D3D9Device;
00387         friend class D3D9DeviceManager;     
00388     };
00389 }
00390 #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 Sun Sep 2 2012 07:27:21