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-2013 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;
00084         bool mAllowDirectX9Ex;
00087         bool mIsDirectX9Ex;
00088 
00090         struct sD3DTextureStageDesc
00091         {
00093             D3D9Mappings::eD3DTexType texType;
00095             size_t coordIndex;
00097             TexCoordCalcMethod autoTexCoordType;
00099             const Frustum *frustum;
00101             IDirect3DBaseTexture9 *pTex;
00103             IDirect3DBaseTexture9 *pVertexTex;
00104         } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
00105 
00106         // Array of up to 8 lights, indexed as per API
00107         // Note that a null value indicates a free slot
00108         Light* mLights[MAX_LIGHTS];     
00109         D3D9DriverList* getDirect3DDrivers();
00110         void refreshD3DSettings();
00111         void refreshFSAAOptions();
00112         
00113         void setD3D9Light( size_t index, Light* light );
00114         
00115         // state management methods, very primitive !!!
00116         HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
00117         HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
00118         HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
00119 
00120         HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
00121         {
00122 #if OGRE_DOUBLE_PRECISION == 1
00123             float temp = static_cast<float>(value);
00124             return __SetRenderState(state, *((LPDWORD)(&temp)));
00125 #else
00126             return __SetRenderState(state, *((LPDWORD)(&value)));
00127 #endif
00128         }
00129 
00131         DWORD _getCurrentAnisotropy(size_t unit);
00133         bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
00134         
00135         D3D9HardwareBufferManager* mHardwareBufferManager;
00136         D3D9GpuProgramManager* mGpuProgramManager;
00137         D3D9HLSLProgramFactory* mHLSLProgramFactory;
00138         D3D9ResourceManager* mResourceManager;
00139         D3D9DeviceManager* mDeviceManager;
00140 
00141         size_t mLastVertexSourceCount;
00142 
00143 
00145         virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
00146         RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
00147 
00149         virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
00150 
00151 
00152         void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
00153         void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
00154         bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
00155         void detachRenderTargetImpl(const String& name);
00156         
00157         HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
00159         Matrix4 mViewMatrix;
00160 
00161         D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
00162     
00163         typedef vector<D3D9RenderWindow*>::type D3D9RenderWindowList;
00164         // List of additional windows after the first (swap chains)
00165         D3D9RenderWindowList mRenderWindows;
00166         
00169         typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
00170         DepthStencilHash mDepthStencilHash;
00171 
00172         MultiheadUseType mMultiheadUse;
00173 
00174     protected:
00175         void setClipPlanesImpl(const PlaneList& clipPlanes);        
00176     public:
00177         // constructor
00178         D3D9RenderSystem( HINSTANCE hInstance );
00179         // destructor
00180         ~D3D9RenderSystem();
00181 
00182         virtual void initConfigOptions();
00183 
00184         // Overridden RenderSystem functions
00185         ConfigOptionMap& getConfigOptions();
00186         String validateConfigOptions();
00187         RenderWindow* _initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window"  );
00189         RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height, 
00190             bool fullScreen, const NameValuePairList *miscParams = 0);
00191         
00193         bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions, 
00194             RenderWindowList& createdWindows);
00195 
00197         DepthBuffer* _createDepthBufferFor( RenderTarget *renderTarget );
00198 
00204         DepthBuffer* _addManualDepthBuffer( IDirect3DDevice9* depthSurfaceDevice, IDirect3DSurface9 *surf );
00205 
00215         using RenderSystem::_cleanupDepthBuffers;
00216         void _cleanupDepthBuffers( IDirect3DDevice9 *creator );
00217 
00225         void _cleanupDepthBuffers( IDirect3DSurface9 *manualSurface );
00226 
00230         void _setRenderTarget(RenderTarget *target);
00231         
00233         virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
00234 
00236         virtual RenderTarget * detachRenderTarget(const String &name);
00237 
00238         String getErrorDescription( long errorNumber ) const;
00239         const String& getName() const;
00240         // Low-level overridden members
00241         void setConfigOption( const String &name, const String &value );
00242         void reinitialise();
00243         void shutdown();
00244         void setAmbientLight( float r, float g, float b );
00245         void setShadingType( ShadeOptions so );
00246         void setLightingEnabled( bool enabled );
00247         void destroyRenderTarget(const String& name);
00248         VertexElementType getColourVertexElementType() const;
00249         void setStencilCheckEnabled(bool enabled);
00250         void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS, 
00251             uint32 refValue = 0, uint32 compareMask = 0xFFFFFFFF, uint32 writeMask = 0xFFFFFFFF,
00252             StencilOperation stencilFailOp = SOP_KEEP, 
00253             StencilOperation depthFailOp = SOP_KEEP,
00254             StencilOperation passOp = SOP_KEEP, 
00255             bool twoSidedOperation = false);
00256         void setNormaliseNormals(bool normalise);
00257 
00258         // Low-level overridden members, mainly for internal use
00259         void _useLights(const LightList& lights, unsigned short limit);
00260         void _setWorldMatrix( const Matrix4 &m );
00261         void _setViewMatrix( const Matrix4 &m );
00262         void _setProjectionMatrix( const Matrix4 &m );
00263         void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
00264         void _setPointSpritesEnabled(bool enabled);
00265         void _setPointParameters(Real size, bool attenuationEnabled, 
00266             Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
00267         void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
00268         void _setVertexTexture(size_t unit, const TexturePtr& tex);
00269         void _disableTextureUnit(size_t texUnit);
00270         void _setTextureCoordSet( size_t unit, size_t index );
00271         void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m, 
00272             const Frustum* frustum = 0);
00273         void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
00274         void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
00275         void _setTextureBorderColour(size_t stage, const ColourValue& colour);
00276         void _setTextureMipmapBias(size_t unit, float bias);
00277         void _setTextureMatrix( size_t unit, const Matrix4 &xform );
00278         void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
00279         void _setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
00280         void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
00281         void _setViewport( Viewport *vp );      
00282         void _beginFrame();
00283         virtual RenderSystemContext* _pauseFrame(void);
00284         virtual void _resumeFrame(RenderSystemContext* context);
00285         void _endFrame();       
00286         void _setCullingMode( CullingMode mode );
00287         void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
00288         void _setDepthBufferCheckEnabled( bool enabled = true );
00289         void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
00290         void _setDepthBufferWriteEnabled(bool enabled = true);
00291         void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
00292         void _setDepthBias(float constantBias, float slopeScaleBias);
00293         void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
00294         void _convertProjectionMatrix(const Matrix4& matrix,
00295             Matrix4& dest, bool forGpuProgram = false);
00296         void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00297             Matrix4& dest, bool forGpuProgram = false);
00298         void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane, 
00299             Real farPlane, Matrix4& dest, bool forGpuProgram = false);
00300         void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, 
00301             Matrix4& dest, bool forGpuProgram = false);
00302         void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane, 
00303             bool forGpuProgram);
00304         void _setPolygonMode(PolygonMode level);
00305         void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
00306         void _setTextureUnitCompareFunction(size_t unit, CompareFunction function);
00307         void _setTextureUnitCompareEnabled(size_t unit, bool compare);
00308         void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
00309         void setVertexDeclaration(VertexDeclaration* decl);
00310         void setVertexDeclaration(VertexDeclaration* decl, bool useGlobalInstancingVertexBufferIsAvailable);
00311         void setVertexBufferBinding(VertexBufferBinding* binding);
00312         void setVertexBufferBinding(VertexBufferBinding* binding, size_t numberOfInstances, bool useGlobalInstancingVertexBufferIsAvailable, bool indexesUsed);
00313         void _render(const RenderOperation& op);
00317         void bindGpuProgram(GpuProgram* prg);
00321         void unbindGpuProgram(GpuProgramType gptype);
00325         void bindGpuProgramParameters(GpuProgramType gptype, 
00326             GpuProgramParametersSharedPtr params, uint16 variabilityMask);
00327         void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
00328 
00329         void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
00330         void clearFrameBuffer(unsigned int buffers, 
00331             const ColourValue& colour = ColourValue::Black, 
00332             Real depth = 1.0f, unsigned short stencil = 0);
00333         void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
00334         void enableClipPlane (ushort index, bool enable);
00335         HardwareOcclusionQuery* createHardwareOcclusionQuery();
00336         Real getHorizontalTexelOffset();
00337         Real getVerticalTexelOffset();
00338         Real getMinimumDepthInputValue();
00339         Real getMaximumDepthInputValue();
00340         void registerThread();
00341         void unregisterThread();
00342         void preExtraThreadsStarted();
00343         void postExtraThreadsStarted();     
00344                 
00345         /*
00346         Returns whether under the current render system buffers marked as TU_STATIC can be locked for update
00347         */
00348         virtual bool isStaticBufferLockable() const { return !mIsDirectX9Ex; }
00349         
00352         static bool isDirectX9Ex()  { return msD3D9RenderSystem->mIsDirectX9Ex; }
00353         
00354         static D3D9ResourceManager* getResourceManager();
00355         static D3D9DeviceManager* getDeviceManager();
00356         static IDirect3D9* getDirect3D9();
00357         static UINT getResourceCreationDeviceCount();
00358         static IDirect3DDevice9* getResourceCreationDevice(UINT index);
00359         static IDirect3DDevice9* getActiveD3D9Device();
00360 
00364         D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
00365 
00369         bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
00370 
00372         void determineFSAASettings(IDirect3DDevice9* d3d9Device, size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat, 
00373             bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
00374 
00376         unsigned int getDisplayMonitorCount() const;
00377 
00379         virtual bool hasAnisotropicMipMapFilter() const { return false; }
00380 
00382         virtual void beginProfileEvent( const String &eventName );
00383 
00385         virtual void endProfileEvent( void );
00386 
00388         virtual void markProfileEvent( const String &eventName );
00389             
00391         void fireDeviceEvent( D3D9Device* device, const String & name );
00392 
00394         MultiheadUseType getMultiheadUse() const { return mMultiheadUse; }
00395     protected:  
00397         DWORD getSamplerId(size_t unit);
00398 
00400         void notifyOnDeviceLost(D3D9Device* device);
00401 
00403         void notifyOnDeviceReset(D3D9Device* device);
00404 
00405     private:
00406         friend class D3D9Device;
00407         friend class D3D9DeviceManager;     
00408     };
00409 }
00410 #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 Mon Jul 27 2020 13:40:42