BsD3D9RenderSystem.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #pragma once
  2. #include "BsD3D9Prerequisites.h"
  3. #include "BsString.h"
  4. #include "BsRenderSystem.h"
  5. #include "BsRenderSystemCapabilities.h"
  6. #include "BsD3D9Mappings.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Implementation of a render system using DirectX 9. Provides abstracted
  11. * access to various low level DX9 methods.
  12. */
  13. class BS_D3D9_EXPORT D3D9RenderSystem : public RenderSystem
  14. {
  15. public:
  16. /**
  17. * @brief Constructs a new instance of the render system using the provided module instance.
  18. */
  19. D3D9RenderSystem(HINSTANCE hInstance);
  20. ~D3D9RenderSystem();
  21. /**
  22. * @copydoc RenderSystem::getName()
  23. */
  24. const String& getName() const;
  25. /**
  26. * @copydoc RenderSystem::getShadingLanguageName()
  27. */
  28. const String& getShadingLanguageName() const;
  29. /**
  30. * @copydoc RenderSystem::setRenderTarget()
  31. */
  32. void setRenderTarget(RenderTargetPtr target);
  33. /**
  34. * @copydoc RenderSystem::bindGpuProgram()
  35. */
  36. void bindGpuProgram(HGpuProgram prg);
  37. /**
  38. * @copydoc RenderSystem::unbindGpuProgram()
  39. */
  40. void unbindGpuProgram(GpuProgramType gptype);
  41. /**
  42. * @copydoc RenderSystem::bindGpuParams()
  43. */
  44. void bindGpuParams(GpuProgramType gptype, GpuParamsPtr params);
  45. /**
  46. * @copydoc RenderSystem::setVertexBuffers()
  47. */
  48. void setVertexBuffers(UINT32 index, VertexBufferPtr* buffers, UINT32 numBuffers);
  49. /**
  50. * @copydoc RenderSystem::setIndexBuffer()
  51. */
  52. void setIndexBuffer(const IndexBufferPtr& buffer);
  53. /**
  54. * @copydoc RenderSystem::setVertexDeclaration()
  55. */
  56. void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration);
  57. /**
  58. * @copydoc RenderSystem::setDrawOperation()
  59. */
  60. void setDrawOperation(DrawOperationType op);
  61. /**
  62. * @copydoc RenderSystem::setSamplerState()
  63. */
  64. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr);
  65. /**
  66. * @copydoc RenderSystem::setSamplerState()
  67. */
  68. void setSamplerState(GpuProgramType gptype, UINT16 unit, const SamplerStatePtr& state);
  69. /**
  70. * @copydoc RenderSystem::setBlendState()
  71. */
  72. void setBlendState(const BlendStatePtr& blendState);
  73. /**
  74. * @copydoc RenderSystem::setRasterizerState()
  75. */
  76. void setRasterizerState(const RasterizerStatePtr& rasterizerState);
  77. /**
  78. * @copydoc RenderSystem::setDepthStencilState()
  79. */
  80. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue);
  81. /**
  82. * @copydoc RenderSystem::setViewport()
  83. */
  84. void setViewport(Viewport vp);
  85. /**
  86. * @copydoc RenderSystem::beginFrame()
  87. */
  88. void beginFrame();
  89. /**
  90. * @copydoc RenderSystem::endFrame()
  91. */
  92. void endFrame();
  93. /**
  94. * @copydoc RenderSystem::draw()
  95. */
  96. void draw(UINT32 vertexOffset, UINT32 vertexCount);
  97. /**
  98. * @copydoc RenderSystem::drawIndexed()
  99. */
  100. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount);
  101. /**
  102. * @copydoc RenderSystem::setScissorRect()
  103. */
  104. void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
  105. /**
  106. * @copydoc RenderSystem::clearRenderTarget()
  107. */
  108. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  109. /**
  110. * @copydoc RenderSystem::clearViewport()
  111. */
  112. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  113. /**
  114. * @copydoc RenderSystem::convertProjectionMatrix()
  115. */
  116. void convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest, bool forGpuProgram = false);
  117. /**
  118. * @copydoc RenderSystem::getHorizontalTexelOffset
  119. */
  120. float getHorizontalTexelOffset();
  121. /**
  122. * @copydoc RenderSystem::getVerticalTexelOffset
  123. */
  124. float getVerticalTexelOffset();
  125. /**
  126. * @copydoc RenderSystem::getMinimumDepthInputValue
  127. */
  128. float getMinimumDepthInputValue();
  129. /**
  130. * @copydoc RenderSystem::getMaximumDepthInputValue
  131. */
  132. float getMaximumDepthInputValue();
  133. /**
  134. * @copydoc RenderSystem::getColorVertexElementType
  135. */
  136. VertexElementType getColorVertexElementType() const;
  137. /************************************************************************/
  138. /* Internal use by DX9 RenderSystem only */
  139. /************************************************************************/
  140. /**
  141. * @brief Returns the resource manager instance.
  142. */
  143. static D3D9ResourceManager* getResourceManager();
  144. /**
  145. * @brief Returns the device manager instance.
  146. */
  147. static D3D9DeviceManager* getDeviceManager();
  148. /**
  149. * @brief Returns the internal DirectX 9 device object.
  150. */
  151. static IDirect3D9* getDirect3D9();
  152. /**
  153. * @brief Returns the number of devices that resources should be created on.
  154. */
  155. static UINT getResourceCreationDeviceCount();
  156. /**
  157. * @brief Returns DirectX 9 device used for resource creation at the specified index.
  158. */
  159. static IDirect3DDevice9* getResourceCreationDevice(UINT index);
  160. /**
  161. * @brief Returns the currently active DirectX 9 device.
  162. */
  163. static IDirect3DDevice9* getActiveD3D9Device();
  164. /**
  165. * @brief Converts engine multisample options into DirectX 9 specific ones.
  166. * Also test for multi-sample support on the device and returns nearest
  167. * supported type if requested type is not available.
  168. *
  169. * @param d3d9Device Device to check for multisampling.
  170. * @param multisampleCount Number of requested samples.
  171. * @param multisampleHint String describing an optional hint to which multisample method to use.
  172. * @param d3dPixelFormat Pixel format used by the render target.
  173. * @param fullscreen Are we testing multisampling for a full-screen render target.
  174. * @param outMultisampleType Output parameter containing DirectX type representing valid multisample type.
  175. * @param outMultisampleQuality Output parameter containing multisample quality.
  176. */
  177. void determineMultisampleSettings(IDirect3DDevice9* d3d9Device, UINT32 multisampleCount, const String& multisampleHint, D3DFORMAT d3dPixelFormat,
  178. bool fullScreen, D3DMULTISAMPLE_TYPE* outMultisampleType, DWORD* outMultisampleQuality) const;
  179. /**
  180. * @brief Register a newly open window with the render system.
  181. */
  182. void registerWindow(RenderWindow& renderWindow);
  183. private:
  184. friend class D3D9Texture;
  185. friend class D3D9RenderWindow;
  186. friend class D3D9Device;
  187. friend class D3D9TextureManager;
  188. friend class D3D9DeviceManager;
  189. friend class D3D9RenderWindowManager;
  190. /**
  191. * @copydoc RenderSystem::initialize_internal
  192. */
  193. void initialize_internal(AsyncOp& asyncOp);
  194. /**
  195. * @copydoc RenderSystem::destroy_internal
  196. */
  197. void destroy_internal();
  198. /**
  199. * @brief Returns a list of available drivers and their properties.
  200. */
  201. D3D9DriverList* getDirect3DDrivers() const;
  202. /**
  203. * @brief Sets DirectX 9 render state option.
  204. */
  205. HRESULT setRenderState(D3DRENDERSTATETYPE state, DWORD value);
  206. /**
  207. * @brief Sets DirectX 9 sampler state option for a sampler at the specified index.
  208. */
  209. HRESULT setSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
  210. /**
  211. * @brief Sets DirectX 9 texture state option for a texture unit at the specified index.
  212. */
  213. HRESULT setTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
  214. /**
  215. * @brief Set a floating point render state option.
  216. */
  217. HRESULT setFloatRenderState(D3DRENDERSTATETYPE state, float value)
  218. {
  219. return setRenderState(state, *((LPDWORD)(&value)));
  220. }
  221. /**
  222. * @brief Returns currently active anisotropy level for the provided texture unit.
  223. */
  224. DWORD getCurrentAnisotropy(UINT32 unit);
  225. /**
  226. * @brief Updates active render system capabilities. Requires active render window to check
  227. * certain capabilities.
  228. *
  229. * @note Also performs an initialization step when called the first time.
  230. */
  231. RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
  232. /**
  233. * @brief Updates render system capabilities with vertex shader related data.
  234. */
  235. void updateVertexShaderCaps(RenderSystemCapabilities* rsc) const;
  236. /**
  237. * @brief Updates render system capabilities with pixel shader related data.
  238. */
  239. void updatePixelShaderCaps(RenderSystemCapabilities* rsc) const;
  240. /**
  241. * @copydoc RenderSystem::setClipPlanesImpl
  242. */
  243. void setClipPlanesImpl(const PlaneList& clipPlanes);
  244. /**
  245. * @brief Converts a HRESULT error number into an error description.
  246. */
  247. String getErrorDescription(long errorNumber) const;
  248. /**
  249. * @brief Sets a clip plane with the specified index.
  250. */
  251. void setClipPlane(UINT16 index, float A, float B, float C, float D);
  252. /**
  253. * @brief Enables or disables a clip plane at the specified index.
  254. */
  255. void enableClipPlane(UINT16 index, bool enable);
  256. /**
  257. * @brief Returns current module instance.
  258. */
  259. HINSTANCE getInstanceHandle() const { return mhInstance; }
  260. /**
  261. * @brief Returns the D3D9 specific mode used for drawing, depending on the
  262. * currently set draw operation.
  263. */
  264. D3DPRIMITIVETYPE getD3D9PrimitiveType() const;
  265. /************************************************************************/
  266. /* Sampler states */
  267. /************************************************************************/
  268. /**
  269. * @brief Sets the texture addressing mode for a texture unit. This determines
  270. * how are UV address values outside of [0, 1] range handled when sampling
  271. * from texture.
  272. */
  273. void setTextureAddressingMode(UINT16 stage, const UVWAddressingMode& uvw);
  274. /**
  275. * @brief Allows you to specify how is the texture bound to the specified texture unit filtered.
  276. * Different filter types are used for different situations like magnifying or minifying a texture.
  277. */
  278. void setTextureFiltering(UINT16 unit, FilterType ftype, FilterOptions filter);
  279. /**
  280. * @brief Sets anisotropy value for the specified texture unit.
  281. */
  282. void setTextureAnisotropy(UINT16 unit, unsigned int maxAnisotropy);
  283. /**
  284. * @brief Sets the texture border color for a texture unit. Border color
  285. * determines color returned by the texture sampler when border addressing mode
  286. * is used and texture address is outside of [0, 1] range.
  287. */
  288. void setTextureBorderColor(UINT16 stage, const Color& color);
  289. /**
  290. * @brief Sets the mipmap bias value for a given texture unit. Bias allows
  291. * you to adjust the mipmap selection calculation. Negative values force a
  292. * larger mipmap to be used, and positive values smaller. Units are in values
  293. * of mip levels, so -1 means use a mipmap one level higher than default.
  294. */
  295. void setTextureMipmapBias(UINT16 unit, float bias);
  296. /************************************************************************/
  297. /* Blend states */
  298. /************************************************************************/
  299. /**
  300. * @brief Sets up blending mode that allows you to combine new pixels with pixels already in the render target.
  301. * Final pixel value = (renderTargetPixel * sourceFactor) op (pixel * destFactor).
  302. */
  303. void setSceneBlending(BlendFactor sourceFactor, BlendFactor destFactor, BlendOperation op);
  304. /**
  305. * @brief Sets up blending mode that allows you to combine new pixels with pixels already in the render target.
  306. * Allows you to set up separate blend operations for alpha values.
  307. *
  308. * Final pixel value = (renderTargetPixel * sourceFactor) op (pixel * destFactor). (And the same for alpha)
  309. */
  310. void setSceneBlending(BlendFactor sourceFactor, BlendFactor destFactor, BlendFactor sourceFactorAlpha,
  311. BlendFactor destFactorAlpha, BlendOperation op, BlendOperation alphaOp);
  312. /**
  313. * @brief Sets alpha test that allows you to reject pixels that fail the comparison function
  314. * versus the provided reference value.
  315. */
  316. void setAlphaTest(CompareFunction func, unsigned char value);
  317. /**
  318. * @brief Enable alpha to coverage. Alpha to coverage allows you to perform blending without needing
  319. * to worry about order of rendering like regular blending does. It requires multi-sampling to
  320. * be active in order to work, and you need to supply an alpha texture that determines object transparency.
  321. */
  322. void setAlphaToCoverage(bool enabled);
  323. /**
  324. * @brief Enables or disables writing to certain color channels of the render target.
  325. */
  326. void setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  327. /************************************************************************/
  328. /* Rasterizer states */
  329. /************************************************************************/
  330. /**
  331. * @brief Sets vertex winding order. Normally you would use this to cull back facing
  332. * polygons.
  333. */
  334. void setCullingMode(CullingMode mode);
  335. /**
  336. * @brief Sets the polygon rasterization mode. Determines how are polygons interpreted.
  337. */
  338. void setPolygonMode(PolygonMode level);
  339. /**
  340. * @brief Sets a depth bias that will offset the depth values of new pixels by the specified amount.
  341. * Final depth bias value is a combination of the constant depth bias and slope depth bias.
  342. * Slope depth bias has more effect the higher the slope of the rendered polygon.
  343. *
  344. * @note This is useful if you want to avoid z fighting for objects at the same or similar depth.
  345. */
  346. void setDepthBias(float constantBias, float slopeScaleBias);
  347. /**
  348. * @brief Scissor test allows you to mask off rendering in all but a given rectangular area
  349. * identified by the rectangle set by setScissorRect().
  350. */
  351. void setScissorTestEnable(bool enable);
  352. /**
  353. * @brief Only applies when rendering to a multisample render target.
  354. * If disabled all of the samples will be taken from the center of the pixel,
  355. * effectively making the image aliased. Default value is true where samples are
  356. * picked randomly within the pixel.
  357. */
  358. void setMultisampleAntialiasEnable(bool enable);
  359. /**
  360. * @brief Only applies when rendering to a non-multisample render target.
  361. * If enabled, lines will be antialiased. Default state is disabled.
  362. */
  363. void setAntialiasedLineEnable(bool enable);
  364. /************************************************************************/
  365. /* Depth stencil state */
  366. /************************************************************************/
  367. /**
  368. * @brief Should new pixels perform depth testing using the set depth comparison function before
  369. * being written.
  370. */
  371. void setDepthBufferCheckEnabled(bool enabled = true);
  372. /**
  373. * @brief Should new pixels write to the depth buffer.
  374. */
  375. void setDepthBufferWriteEnabled(bool enabled = true);
  376. /**
  377. * @brief Sets comparison function used for depth testing. Determines how are new and existing
  378. * pixel values compared - if comparison function returns true the new pixel is written.
  379. */
  380. void setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  381. /**
  382. * @brief Turns stencil tests on or off. By default this is disabled.
  383. * Stencil testing allow you to mask out a part of the rendered image by using
  384. * various stencil operations provided.
  385. */
  386. void setStencilCheckEnabled(bool enabled);
  387. /**
  388. * @brief Allows you to set stencil operations that are performed when stencil test passes or fails.
  389. *
  390. * @param stencilFailOp Operation executed when stencil test fails.
  391. * @param depthFailOp Operation executed when stencil test succeeds but depth test fails.
  392. * @param passOp Operation executed when stencil test succeeds and depth test succeeds.
  393. * @param front Should the stencil operations be applied to front or back facing polygons.
  394. */
  395. void setStencilBufferOperations(StencilOperation stencilFailOp = SOP_KEEP,
  396. StencilOperation depthFailOp = SOP_KEEP, StencilOperation passOp = SOP_KEEP,
  397. bool ccw = true);
  398. /**
  399. * @brief Sets a stencil buffer comparison function. The result of this will cause one of 3 actions
  400. * depending on whether the test fails, succeeds but with the depth buffer check still failing,
  401. * or succeeds with the depth buffer check passing too.
  402. *
  403. * @param func Comparison function that determines whether a stencil test fails or passes. Reference value
  404. * gets compared to the value already in the buffer using this function.
  405. * @param ccw If set to true, the stencil operations will be applied to counterclockwise
  406. * faces. Otherwise they will be applied to clockwise faces.
  407. */
  408. void setStencilBufferFunc(CompareFunction func = CMPF_ALWAYS_PASS, bool ccw = true);
  409. /**
  410. * @brief The bitmask applied to both the stencil value and the reference value
  411. * before comparison.
  412. */
  413. void setStencilBufferReadMask(UINT32 mask = 0xFFFFFFFF);
  414. /**
  415. * @brief The bitmask applied to the stencil value before writing it to the stencil buffer.
  416. */
  417. void setStencilBufferWriteMask(UINT32 mask = 0xFFFFFFFF);
  418. /**
  419. * @brief Sets a reference values used for stencil buffer comparisons.
  420. * Actual comparison function and stencil operations are set by setting the DepthStencilState.
  421. */
  422. void setStencilRefValue(UINT32 refValue);
  423. /**
  424. * @brief Clears an area of the currently active render target.
  425. *
  426. * @param buffers Combination of one or more elements of FrameBufferType
  427. * denoting which buffers are to be cleared.
  428. * @param color (optional) The color to clear the color buffer with, if enabled.
  429. * @param depth (optional) The value to initialize the depth buffer with, if enabled.
  430. * @param stencil (optional) The value to initialize the stencil buffer with, if enabled.
  431. * @param clearArea (optional) Area in pixels to clear.
  432. */
  433. void clearArea(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const RectI& clearArea = RectI::EMPTY);
  434. /**
  435. * @brief Triggered when device has been lost.
  436. */
  437. void notifyOnDeviceLost(D3D9Device* device);
  438. /**
  439. * @brief Triggered when device is being reset.
  440. */
  441. void notifyOnDeviceReset(D3D9Device* device);
  442. private:
  443. /**
  444. * @brief Holds texture unit settings.
  445. */
  446. struct sD3DTextureStageDesc
  447. {
  448. D3D9Mappings::D3DTexType texType;
  449. size_t coordIndex;
  450. IDirect3DBaseTexture9 *pTex;
  451. IDirect3DBaseTexture9 *pVertexTex;
  452. };
  453. static D3D9RenderSystem* msD3D9RenderSystem;
  454. IDirect3D9* mpD3D;
  455. D3D9HLSLProgramFactory* mHLSLProgramFactory;
  456. D3D9ResourceManager* mResourceManager;
  457. D3D9DeviceManager* mDeviceManager;
  458. mutable D3D9DriverList* mDriverList;
  459. D3D9Driver* mActiveD3DDriver;
  460. UINT32 mNumTexStages;
  461. sD3DTextureStageDesc* mTexStageDesc;
  462. bool mIsFrameInProgress;
  463. bool mRestoreFrameOnReset;
  464. HINSTANCE mhInstance;
  465. UINT32 mViewportLeft, mViewportTop, mViewportWidth, mViewportHeight;
  466. RECT mScissorRect;
  467. DrawOperationType mCurrentDrawOperation;
  468. };
  469. }