[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 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 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __D3D9RENDERSYSTEM_H__ |
---|
| 30 | #define __D3D9RENDERSYSTEM_H__ |
---|
| 31 | |
---|
| 32 | #include "OgreD3D9Prerequisites.h" |
---|
| 33 | #include "OgreString.h" |
---|
| 34 | #include "OgreStringConverter.h" |
---|
| 35 | #include "OgreRenderSystem.h" |
---|
| 36 | #include "OgreD3D9Mappings.h" |
---|
| 37 | |
---|
| 38 | #include "OgreNoMemoryMacros.h" |
---|
| 39 | #include <d3d9.h> |
---|
| 40 | #include <d3dx9.h> |
---|
| 41 | #include <dxerr9.h> |
---|
| 42 | #include "OgreMemoryMacros.h" |
---|
| 43 | |
---|
| 44 | namespace Ogre |
---|
| 45 | { |
---|
| 46 | #define MAX_LIGHTS 8 |
---|
| 47 | |
---|
| 48 | class D3D9DriverList; |
---|
| 49 | class D3D9Driver; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | Implementation of DirectX9 as a rendering system. |
---|
| 53 | */ |
---|
| 54 | class D3D9RenderSystem : public RenderSystem |
---|
| 55 | { |
---|
| 56 | private: |
---|
| 57 | /// Direct3D |
---|
| 58 | LPDIRECT3D9 mpD3D; |
---|
| 59 | /// Direct3D rendering device |
---|
| 60 | LPDIRECT3DDEVICE9 mpD3DDevice; |
---|
| 61 | |
---|
| 62 | // Stored options |
---|
| 63 | ConfigOptionMap mOptions; |
---|
| 64 | /// full-screen multisampling antialiasing type |
---|
| 65 | D3DMULTISAMPLE_TYPE mFSAAType; |
---|
| 66 | /// full-screen multisampling antialiasing level |
---|
| 67 | DWORD mFSAAQuality; |
---|
| 68 | |
---|
| 69 | /// instance |
---|
| 70 | HINSTANCE mhInstance; |
---|
| 71 | |
---|
| 72 | /// List of D3D drivers installed (video cards) |
---|
| 73 | D3D9DriverList* mDriverList; |
---|
| 74 | /// Currently active driver |
---|
| 75 | D3D9Driver* mActiveD3DDriver; |
---|
| 76 | /// Device caps. |
---|
| 77 | D3DCAPS9 mCaps; |
---|
| 78 | /// NVPerfHUD allowed? |
---|
| 79 | bool mUseNVPerfHUD; |
---|
| 80 | /// Per-stage constant support? (not in main caps since D3D specific & minor) |
---|
| 81 | bool mPerStageConstantSupport; |
---|
| 82 | |
---|
| 83 | /// structure holding texture unit settings for every stage |
---|
| 84 | struct sD3DTextureStageDesc |
---|
| 85 | { |
---|
| 86 | /// the type of the texture |
---|
| 87 | D3D9Mappings::eD3DTexType texType; |
---|
| 88 | /// wich texCoordIndex to use |
---|
| 89 | size_t coordIndex; |
---|
| 90 | /// type of auto tex. calc. used |
---|
| 91 | TexCoordCalcMethod autoTexCoordType; |
---|
| 92 | /// Frustum, used if the above is projection |
---|
| 93 | const Frustum *frustum; |
---|
| 94 | /// texture |
---|
| 95 | IDirect3DBaseTexture9 *pTex; |
---|
| 96 | /// vertex texture |
---|
| 97 | IDirect3DBaseTexture9 *pVertexTex; |
---|
| 98 | } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS]; |
---|
| 99 | |
---|
| 100 | // Array of up to 8 lights, indexed as per API |
---|
| 101 | // Note that a null value indeicates a free slot |
---|
| 102 | Light* mLights[MAX_LIGHTS]; |
---|
| 103 | |
---|
| 104 | D3D9DriverList* getDirect3DDrivers(void); |
---|
| 105 | void refreshD3DSettings(void); |
---|
| 106 | void refreshFSAAOptions(void); |
---|
| 107 | void freeDevice(void); |
---|
| 108 | |
---|
| 109 | inline bool compareDecls( D3DVERTEXELEMENT9* pDecl1, D3DVERTEXELEMENT9* pDecl2, size_t size ); |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | void initInputDevices(void); |
---|
| 113 | void processInputDevices(void); |
---|
| 114 | void setD3D9Light( size_t index, Light* light ); |
---|
| 115 | |
---|
| 116 | // state management methods, very primitive !!! |
---|
| 117 | HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value); |
---|
| 118 | HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value); |
---|
| 119 | HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value); |
---|
| 120 | |
---|
| 121 | HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value) |
---|
| 122 | { |
---|
| 123 | #if OGRE_DOUBLE_PRECISION == 1 |
---|
| 124 | float temp = static_cast<float>(value); |
---|
| 125 | return __SetRenderState(state, *((LPDWORD)(&temp))); |
---|
| 126 | #else |
---|
| 127 | return __SetRenderState(state, *((LPDWORD)(&value))); |
---|
| 128 | #endif |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /// return anisotropy level |
---|
| 132 | DWORD _getCurrentAnisotropy(size_t unit); |
---|
| 133 | /// check if a FSAA is supported |
---|
| 134 | bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen); |
---|
| 135 | /// set FSAA |
---|
| 136 | void _setFSAA(D3DMULTISAMPLE_TYPE type, DWORD qualityLevel); |
---|
| 137 | |
---|
| 138 | D3D9HardwareBufferManager* mHardwareBufferManager; |
---|
| 139 | D3D9GpuProgramManager* mGpuProgramManager; |
---|
| 140 | D3D9HLSLProgramFactory* mHLSLProgramFactory; |
---|
| 141 | |
---|
| 142 | size_t mLastVertexSourceCount; |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | /// Internal method for populating the capabilities structure |
---|
| 146 | void initCapabilities(void); |
---|
| 147 | |
---|
| 148 | void convertVertexShaderCaps(void); |
---|
| 149 | void convertPixelShaderCaps(void); |
---|
| 150 | bool checkVertexTextureFormats(void); |
---|
| 151 | |
---|
| 152 | unsigned short mCurrentLights; |
---|
| 153 | /// Saved last view matrix |
---|
| 154 | Matrix4 mViewMatrix; |
---|
| 155 | |
---|
| 156 | // What follows is a set of duplicated lists just to make it |
---|
| 157 | // easier to deal with lost devices |
---|
| 158 | |
---|
| 159 | /// Primary window, the one used to create the device |
---|
| 160 | D3D9RenderWindow* mPrimaryWindow; |
---|
| 161 | |
---|
| 162 | typedef std::vector<D3D9RenderWindow*> SecondaryWindowList; |
---|
| 163 | // List of additional windows after the first (swap chains) |
---|
| 164 | SecondaryWindowList mSecondaryWindows; |
---|
| 165 | |
---|
| 166 | bool mDeviceLost; |
---|
| 167 | bool mBasicStatesInitialised; |
---|
| 168 | |
---|
| 169 | /** Mapping of texture format -> DepthStencil. Used as cache by _getDepthStencilFormatFor |
---|
| 170 | */ |
---|
| 171 | typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash; |
---|
| 172 | DepthStencilHash mDepthStencilHash; |
---|
| 173 | |
---|
| 174 | /** Mapping of depthstencil format -> depthstencil buffer |
---|
| 175 | Keep one depthstencil buffer around for every format that is used, it must be large |
---|
| 176 | enough to hold the largest rendering target. |
---|
| 177 | This is used as cache by _getDepthStencilFor. |
---|
| 178 | */ |
---|
| 179 | typedef std::pair<D3DFORMAT, D3DMULTISAMPLE_TYPE> ZBufferFormat; |
---|
| 180 | struct ZBufferRef |
---|
| 181 | { |
---|
| 182 | IDirect3DSurface9 *surface; |
---|
| 183 | size_t width, height; |
---|
| 184 | }; |
---|
| 185 | typedef std::map<ZBufferFormat, ZBufferRef> ZBufferHash; |
---|
| 186 | ZBufferHash mZBufferHash; |
---|
| 187 | public: |
---|
| 188 | // constructor |
---|
| 189 | D3D9RenderSystem( HINSTANCE hInstance ); |
---|
| 190 | // destructor |
---|
| 191 | ~D3D9RenderSystem(); |
---|
| 192 | |
---|
| 193 | virtual void initConfigOptions(void); |
---|
| 194 | |
---|
| 195 | // Overridden RenderSystem functions |
---|
| 196 | ConfigOptionMap& getConfigOptions(void); |
---|
| 197 | String validateConfigOptions(void); |
---|
| 198 | RenderWindow* initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window" ); |
---|
| 199 | /// @copydoc RenderSystem::createRenderWindow |
---|
| 200 | RenderWindow* createRenderWindow(const String &name, unsigned int width, unsigned int height, |
---|
| 201 | bool fullScreen, const NameValuePairList *miscParams = 0); |
---|
| 202 | |
---|
| 203 | /// @copydoc RenderSystem::createRenderTexture |
---|
| 204 | RenderTexture * createRenderTexture( const String & name, unsigned int width, unsigned int height, |
---|
| 205 | TextureType texType = TEX_TYPE_2D, PixelFormat internalFormat = PF_X8R8G8B8, |
---|
| 206 | const NameValuePairList *miscParams = 0 ); |
---|
| 207 | |
---|
| 208 | /// @copydoc RenderSystem::createMultiRenderTarget |
---|
| 209 | virtual MultiRenderTarget * createMultiRenderTarget(const String & name); |
---|
| 210 | |
---|
| 211 | String getErrorDescription( long errorNumber ) const; |
---|
| 212 | const String& getName(void) const; |
---|
| 213 | // Low-level overridden members |
---|
| 214 | void setConfigOption( const String &name, const String &value ); |
---|
| 215 | void reinitialise(); |
---|
| 216 | void shutdown(); |
---|
| 217 | void setAmbientLight( float r, float g, float b ); |
---|
| 218 | void setShadingType( ShadeOptions so ); |
---|
| 219 | void setLightingEnabled( bool enabled ); |
---|
| 220 | void destroyRenderTarget(const String& name); |
---|
| 221 | VertexElementType getColourVertexElementType(void) const; |
---|
| 222 | void setStencilCheckEnabled(bool enabled); |
---|
| 223 | void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS, |
---|
| 224 | uint32 refValue = 0, uint32 mask = 0xFFFFFFFF, |
---|
| 225 | StencilOperation stencilFailOp = SOP_KEEP, |
---|
| 226 | StencilOperation depthFailOp = SOP_KEEP, |
---|
| 227 | StencilOperation passOp = SOP_KEEP, |
---|
| 228 | bool twoSidedOperation = false); |
---|
| 229 | void setNormaliseNormals(bool normalise); |
---|
| 230 | |
---|
| 231 | // Low-level overridden members, mainly for internal use |
---|
| 232 | void _useLights(const LightList& lights, unsigned short limit); |
---|
| 233 | void _setWorldMatrix( const Matrix4 &m ); |
---|
| 234 | void _setViewMatrix( const Matrix4 &m ); |
---|
| 235 | void _setProjectionMatrix( const Matrix4 &m ); |
---|
| 236 | void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking ); |
---|
| 237 | void _setPointSpritesEnabled(bool enabled); |
---|
| 238 | void _setPointParameters(Real size, bool attenuationEnabled, |
---|
| 239 | Real constant, Real linear, Real quadratic, Real minSize, Real maxSize); |
---|
| 240 | void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr); |
---|
| 241 | void _setVertexTexture(size_t unit, const TexturePtr& tex); |
---|
| 242 | void _disableTextureUnit(size_t texUnit); |
---|
| 243 | void _setTextureCoordSet( size_t unit, size_t index ); |
---|
| 244 | void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m, |
---|
| 245 | const Frustum* frustum = 0); |
---|
| 246 | void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm ); |
---|
| 247 | void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw); |
---|
| 248 | void _setTextureBorderColour(size_t stage, const ColourValue& colour); |
---|
| 249 | void _setTextureMipmapBias(size_t unit, float bias); |
---|
| 250 | void _setTextureMatrix( size_t unit, const Matrix4 &xform ); |
---|
| 251 | void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor ); |
---|
| 252 | void _setAlphaRejectSettings( CompareFunction func, unsigned char value ); |
---|
| 253 | void _setViewport( Viewport *vp ); |
---|
| 254 | void _beginFrame(void); |
---|
| 255 | void _endFrame(void); |
---|
| 256 | void _setCullingMode( CullingMode mode ); |
---|
| 257 | void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL ); |
---|
| 258 | void _setDepthBufferCheckEnabled( bool enabled = true ); |
---|
| 259 | void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha); |
---|
| 260 | void _setDepthBufferWriteEnabled(bool enabled = true); |
---|
| 261 | void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL ); |
---|
| 262 | void _setDepthBias(float constantBias, float slopeScaleBias); |
---|
| 263 | void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 ); |
---|
| 264 | void _convertProjectionMatrix(const Matrix4& matrix, |
---|
| 265 | Matrix4& dest, bool forGpuProgram = false); |
---|
| 266 | void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, |
---|
| 267 | Matrix4& dest, bool forGpuProgram = false); |
---|
| 268 | void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane, |
---|
| 269 | Real farPlane, Matrix4& dest, bool forGpuProgram = false); |
---|
| 270 | void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane, |
---|
| 271 | Matrix4& dest, bool forGpuProgram = false); |
---|
| 272 | void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane, |
---|
| 273 | bool forGpuProgram); |
---|
| 274 | void _setPolygonMode(PolygonMode level); |
---|
| 275 | void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter); |
---|
| 276 | void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy); |
---|
| 277 | void setVertexDeclaration(VertexDeclaration* decl); |
---|
| 278 | void setVertexBufferBinding(VertexBufferBinding* binding); |
---|
| 279 | void _render(const RenderOperation& op); |
---|
| 280 | /** See |
---|
| 281 | RenderSystem |
---|
| 282 | */ |
---|
| 283 | void bindGpuProgram(GpuProgram* prg); |
---|
| 284 | /** See |
---|
| 285 | RenderSystem |
---|
| 286 | */ |
---|
| 287 | void unbindGpuProgram(GpuProgramType gptype); |
---|
| 288 | /** See |
---|
| 289 | RenderSystem |
---|
| 290 | */ |
---|
| 291 | void bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params); |
---|
| 292 | /** See |
---|
| 293 | RenderSystem |
---|
| 294 | */ |
---|
| 295 | void bindGpuProgramPassIterationParameters(GpuProgramType gptype); |
---|
| 296 | /** See |
---|
| 297 | RenderSystem |
---|
| 298 | */ |
---|
| 299 | void setClipPlanes(const PlaneList& clipPlanes); |
---|
| 300 | |
---|
| 301 | void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600); |
---|
| 302 | void clearFrameBuffer(unsigned int buffers, |
---|
| 303 | const ColourValue& colour = ColourValue::Black, |
---|
| 304 | Real depth = 1.0f, unsigned short stencil = 0); |
---|
| 305 | void setClipPlane (ushort index, Real A, Real B, Real C, Real D); |
---|
| 306 | void enableClipPlane (ushort index, bool enable); |
---|
| 307 | HardwareOcclusionQuery* createHardwareOcclusionQuery(void); |
---|
| 308 | Real getHorizontalTexelOffset(void); |
---|
| 309 | Real getVerticalTexelOffset(void); |
---|
| 310 | Real getMinimumDepthInputValue(void); |
---|
| 311 | Real getMaximumDepthInputValue(void); |
---|
| 312 | void registerThread(); |
---|
| 313 | void unregisterThread(); |
---|
| 314 | void preExtraThreadsStarted(); |
---|
| 315 | void postExtraThreadsStarted(); |
---|
| 316 | |
---|
| 317 | /** D3D specific method to restore a lost device. */ |
---|
| 318 | void restoreLostDevice(void); |
---|
| 319 | /** D3D specific method to return whether the device has been lost. */ |
---|
| 320 | bool isDeviceLost(void); |
---|
| 321 | /** Notify that a device has been lost */ |
---|
| 322 | void _notifyDeviceLost(void); |
---|
| 323 | |
---|
| 324 | /** Check which depthStencil formats can be used with a certain pixel format, |
---|
| 325 | and return the best suited. |
---|
| 326 | */ |
---|
| 327 | D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt); |
---|
| 328 | |
---|
| 329 | /** Get a depth stencil surface that is compatible with an internal pixel format and |
---|
| 330 | multisample type. |
---|
| 331 | @returns A directx surface, or 0 if there is no compatible depthstencil possible. |
---|
| 332 | */ |
---|
| 333 | IDirect3DSurface9* _getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, size_t width, size_t height); |
---|
| 334 | |
---|
| 335 | /** Clear all cached depth stencil surfaces |
---|
| 336 | */ |
---|
| 337 | void _cleanupDepthStencils(); |
---|
| 338 | |
---|
| 339 | /** Check whether or not filtering is supported for the precise texture format requested |
---|
| 340 | with the given usage options. |
---|
| 341 | */ |
---|
| 342 | bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage); |
---|
| 343 | |
---|
| 344 | }; |
---|
| 345 | } |
---|
| 346 | #endif |
---|