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