CmD3D9RenderSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __D3D9RENDERSYSTEM_H__
  25. #define __D3D9RENDERSYSTEM_H__
  26. #include "CmD3D9Prerequisites.h"
  27. #include "CmString.h"
  28. #include "CmConfigOptionMap.h"
  29. #include "CmRenderSystem.h"
  30. #include "CmRenderSystemCapabilities.h"
  31. #include "CmD3D9Mappings.h"
  32. #include "CmCgProgramFactory.h"
  33. namespace CamelotEngine
  34. {
  35. class D3D9DriverList;
  36. class D3D9Driver;
  37. class D3D9Device;
  38. class D3D9DeviceManager;
  39. class D3D9ResourceManager;
  40. /**
  41. Implementation of DirectX9 as a rendering system.
  42. */
  43. class CM_D3D9_EXPORT D3D9RenderSystem : public RenderSystem
  44. {
  45. private:
  46. /// Direct3D
  47. IDirect3D9* mpD3D;
  48. // Stored options
  49. ConfigOptionMap mOptions;
  50. size_t mFSAASamples;
  51. String mFSAAHint;
  52. /// instance
  53. HINSTANCE mhInstance;
  54. /// List of D3D drivers installed (video cards)
  55. D3D9DriverList* mDriverList;
  56. /// Currently active driver
  57. D3D9Driver* mActiveD3DDriver;
  58. /// NVPerfHUD allowed?
  59. bool mUseNVPerfHUD;
  60. /// Per-stage constant support? (not in main caps since D3D specific & minor)
  61. bool mPerStageConstantSupport;
  62. /// Fast singleton access.
  63. static D3D9RenderSystem* msD3D9RenderSystem;
  64. /// structure holding texture unit settings for every stage
  65. struct sD3DTextureStageDesc
  66. {
  67. /// the type of the texture
  68. D3D9Mappings::eD3DTexType texType;
  69. /// which texCoordIndex to use
  70. size_t coordIndex;
  71. /// texture
  72. IDirect3DBaseTexture9 *pTex;
  73. /// vertex texture
  74. IDirect3DBaseTexture9 *pVertexTex;
  75. } mTexStageDesc[CM_MAX_TEXTURE_LAYERS];
  76. D3D9DriverList* getDirect3DDrivers();
  77. void refreshD3DSettings();
  78. void refreshFSAAOptions();
  79. // state management methods, very primitive !!!
  80. HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
  81. HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
  82. HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
  83. HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, float value)
  84. {
  85. #if OGRE_DOUBLE_PRECISION == 1
  86. float temp = static_cast<float>(value);
  87. return __SetRenderState(state, *((LPDWORD)(&temp)));
  88. #else
  89. return __SetRenderState(state, *((LPDWORD)(&value)));
  90. #endif
  91. }
  92. /// return anisotropy level
  93. DWORD _getCurrentAnisotropy(UINT32 unit);
  94. /// check if a FSAA is supported
  95. bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
  96. D3D9HLSLProgramFactory* mHLSLProgramFactory;
  97. CgProgramFactory* mCgProgramFactory;
  98. D3D9ResourceManager* mResourceManager;
  99. D3D9DeviceManager* mDeviceManager;
  100. size_t mLastVertexSourceCount;
  101. /// Internal method for populating the capabilities structure
  102. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
  103. RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
  104. /** See RenderSystem definition */
  105. virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
  106. void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
  107. void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
  108. bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
  109. HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
  110. /// Saved last view matrix
  111. Matrix4 mViewMatrix;
  112. D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
  113. typedef vector<D3D9RenderWindow*>::type D3D9RenderWindowList;
  114. // List of additional windows after the first (swap chains)
  115. D3D9RenderWindowList mRenderWindows;
  116. /** Mapping of texture format -> DepthStencil. Used as cache by _getDepthStencilFormatFor
  117. */
  118. typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
  119. DepthStencilHash mDepthStencilHash;
  120. /** Mapping of depthstencil format -> depthstencil buffer
  121. Keep one depthstencil buffer around for every format that is used, it must be large
  122. enough to hold the largest rendering target.
  123. This is used as cache by _getDepthStencilFor.
  124. */
  125. struct ZBufferIdentifier
  126. {
  127. IDirect3DDevice9* device;
  128. D3DFORMAT format;
  129. D3DMULTISAMPLE_TYPE multisampleType;
  130. };
  131. struct ZBufferRef
  132. {
  133. IDirect3DSurface9 *surface;
  134. size_t width, height;
  135. };
  136. struct ZBufferIdentifierComparator
  137. {
  138. bool operator()(const ZBufferIdentifier& z0, const ZBufferIdentifier& z1) const;
  139. };
  140. typedef deque<ZBufferRef>::type ZBufferRefQueue;
  141. typedef map<ZBufferIdentifier, ZBufferRefQueue, ZBufferIdentifierComparator>::type ZBufferHash;
  142. ZBufferHash mZBufferHash;
  143. protected:
  144. // I know that's a lot of friends, but I'd rather have friend classes than exposing the needed methods
  145. // as public interface.
  146. friend class D3D9Texture;
  147. friend class D3D9RenderWindow;
  148. friend class D3D9Device;
  149. friend class D3D9TextureManager;
  150. void setClipPlanesImpl(const PlaneList& clipPlanes);
  151. /// @copydoc RenderSystem::createMultiRenderTarget
  152. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  153. String getErrorDescription( long errorNumber ) const;
  154. void initConfigOptions();
  155. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  156. void enableClipPlane (UINT16 index, bool enable);
  157. /**
  158. Get the matching Z-Buffer identifier for a certain render target
  159. */
  160. ZBufferIdentifier getZBufferIdentifier(RenderTarget* rt);
  161. /** Check which depthStencil formats can be used with a certain pixel format,
  162. and return the best suited.
  163. */
  164. D3DFORMAT getDepthStencilFormatFor(D3DFORMAT fmt);
  165. /** Get a depth stencil surface that is compatible with an internal pixel format and
  166. multisample type.
  167. @returns A directx surface, or 0 if there is no compatible depthstencil possible.
  168. */
  169. IDirect3DSurface9* getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, DWORD multisample_quality, UINT32 width, UINT32 height);
  170. /** Clear all cached depth stencil surfaces
  171. */
  172. void cleanupDepthStencils(IDirect3DDevice9* d3d9Device);
  173. /** Check whether or not filtering is supported for the precise texture format requested
  174. with the given usage options.
  175. */
  176. bool checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
  177. /// Take in some requested FSAA settings and output supported D3D settings
  178. void determineFSAASettings(IDirect3DDevice9* d3d9Device, UINT32 fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat,
  179. bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
  180. public:
  181. // constructor
  182. D3D9RenderSystem( HINSTANCE hInstance );
  183. // destructor
  184. ~D3D9RenderSystem();
  185. const String& getName() const;
  186. void shutdown();
  187. void setStencilCheckEnabled_internal(bool enabled);
  188. void setStencilBufferParams_internal(CompareFunction func = CMPF_ALWAYS_PASS,
  189. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  190. StencilOperation stencilFailOp = SOP_KEEP,
  191. StencilOperation depthFailOp = SOP_KEEP,
  192. StencilOperation passOp = SOP_KEEP,
  193. bool twoSidedOperation = false);
  194. void startUp_internal();
  195. void createRenderWindow_internal(const String &name, unsigned int width, unsigned int height,
  196. bool fullScreen, const NameValuePairList& miscParams, AsyncOp& asyncOp);
  197. void destroyRenderTarget_internal(RenderTarget* renderTarget);
  198. void setRenderTarget_internal(RenderTarget *target);
  199. void bindGpuProgram_internal(GpuProgramHandle prg);
  200. void unbindGpuProgram_internal(GpuProgramType gptype);
  201. void bindGpuProgramParameters_internal(GpuProgramType gptype,
  202. GpuProgramParametersSharedPtr params, UINT16 variabilityMask);
  203. // Low-level overridden members, mainly for internal use
  204. void setPointParameters_internal(float size, bool attenuationEnabled,
  205. float constant, float linear, float quadratic, float minSize, float maxSize);
  206. void setTexture_internal(UINT16 unit, bool enabled, const TexturePtr &texPtr);
  207. void setVertexTexture_internal(UINT16 unit, const TexturePtr& tex);
  208. void disableTextureUnit_internal(UINT16 texUnit);
  209. void setTextureAddressingMode_internal(UINT16 stage, const SamplerState::UVWAddressingMode& uvw);
  210. void setTextureBorderColor_internal(UINT16 stage, const Color& colour);
  211. void setTextureMipmapBias_internal(UINT16 unit, float bias);
  212. void setSceneBlending_internal( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  213. void setSeparateSceneBlending_internal( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  214. void setAlphaRejectSettings_internal( CompareFunction func, unsigned char value, bool alphaToCoverage );
  215. void setViewport_internal(const Viewport& vp);
  216. void beginFrame_internal();
  217. void endFrame_internal();
  218. void setCullingMode_internal( CullingMode mode );
  219. void setDepthBufferParams_internal( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
  220. void setDepthBufferCheckEnabled_internal( bool enabled = true );
  221. void setColorBufferWriteEnabled_internal(bool red, bool green, bool blue, bool alpha);
  222. void setDepthBufferWriteEnabled_internal(bool enabled = true);
  223. void setDepthBufferFunction_internal( CompareFunction func = CMPF_LESS_EQUAL );
  224. void setDepthBias_internal(float constantBias, float slopeScaleBias);
  225. void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest, bool forGpuProgram = false);
  226. void setPolygonMode_internal(PolygonMode level);
  227. void setTextureFiltering_internal(UINT16 unit, FilterType ftype, FilterOptions filter);
  228. void setTextureAnisotropy_internal(UINT16 unit, unsigned int maxAnisotropy);
  229. void setVertexDeclaration_internal(VertexDeclarationPtr decl);
  230. void setVertexBufferBinding_internal(VertexBufferBinding* binding);
  231. void render_internal(const RenderOperation& op);
  232. void setScissorTest_internal(bool enabled, UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600);
  233. void clearFrameBuffer_internal(unsigned int buffers,
  234. const Color& colour = Color::Black,
  235. float depth = 1.0f, unsigned short stencil = 0);
  236. float getHorizontalTexelOffset();
  237. float getVerticalTexelOffset();
  238. float getMinimumDepthInputValue();
  239. float getMaximumDepthInputValue();
  240. VertexElementType getColorVertexElementType() const;
  241. static D3D9ResourceManager* getResourceManager();
  242. static D3D9DeviceManager* getDeviceManager();
  243. static IDirect3D9* getDirect3D9();
  244. static UINT getResourceCreationDeviceCount();
  245. static IDirect3DDevice9* getResourceCreationDevice(UINT index);
  246. static IDirect3DDevice9* getActiveD3D9Device();
  247. protected:
  248. /// Notify when a device has been lost.
  249. void notifyOnDeviceLost(D3D9Device* device);
  250. /// Notify when a device has been reset.
  251. void notifyOnDeviceReset(D3D9Device* device);
  252. typedef map<RenderTarget*, ZBufferRef>::type TargetDepthStencilMap;
  253. TargetDepthStencilMap mCheckedOutTextures;
  254. private:
  255. friend class D3D9Device;
  256. friend class D3D9DeviceManager;
  257. };
  258. }
  259. #endif