BsRasterizerState.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsIReflectable.h"
  6. #include "BsCoreObject.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup RenderAPI
  10. * @{
  11. */
  12. /** Structure that describes pipeline rasterizer state. Used for initializing a RasterizerState. */
  13. struct BS_CORE_EXPORT RASTERIZER_STATE_DESC
  14. {
  15. RASTERIZER_STATE_DESC()
  16. : polygonMode(PM_SOLID)
  17. , cullMode(CULL_COUNTERCLOCKWISE)
  18. , depthBias(0)
  19. , depthBiasClamp(0.0f)
  20. , slopeScaledDepthBias(0.0f)
  21. , depthClipEnable(true)
  22. , scissorEnable(false)
  23. , multisampleEnable(true)
  24. , antialiasedLineEnable(false)
  25. { }
  26. bool operator==(const RASTERIZER_STATE_DESC& rhs) const;
  27. PolygonMode polygonMode;
  28. CullingMode cullMode;
  29. float depthBias;
  30. float depthBiasClamp;
  31. float slopeScaledDepthBias;
  32. bool depthClipEnable;
  33. bool scissorEnable;
  34. bool multisampleEnable;
  35. bool antialiasedLineEnable;
  36. };
  37. /** Properties of RasterizerState. Shared between sim and core thread versions of RasterizerState. */
  38. class BS_CORE_EXPORT RasterizerProperties
  39. {
  40. public:
  41. RasterizerProperties(const RASTERIZER_STATE_DESC& desc);
  42. /** Polygon mode allows you to draw polygons as solid objects or as wireframe by just drawing their edges. */
  43. PolygonMode getPolygonMode() const { return mData.polygonMode; }
  44. /**
  45. * Sets vertex winding order. Faces that contain vertices with this order will be culled and not rasterized. Used
  46. * primarily for saving cycles by not rendering backfacing faces.
  47. */
  48. CullingMode getCullMode() const { return mData.cullMode; }
  49. /**
  50. * Represents a constant depth bias that will offset the depth values of new pixels by the specified amount.
  51. *
  52. * @note This is useful if you want to avoid z fighting for objects at the same or similar depth.
  53. */
  54. float getDepthBias() const { return mData.depthBias; }
  55. /** Maximum depth bias value. */
  56. float getDepthBiasClamp() const { return mData.depthBiasClamp; }
  57. /**
  58. * Represents a dynamic depth bias that increases as the slope of the rendered polygons surface increases.
  59. * Resulting value offsets depth values of new pixels. This offset will be added on top of the constant depth bias.
  60. *
  61. * @note This is useful if you want to avoid z fighting for objects at the same or similar depth.
  62. */
  63. float getSlopeScaledDepthBias() const { return mData.slopeScaledDepthBias; }
  64. /**
  65. * If true, clipping of polygons past the far Z plane is enabled. This ensures proper Z ordering for polygons
  66. * outside of valid depth range (otherwise they all have the same depth). It can be useful to disable if you are
  67. * performing stencil operations that count on objects having a front and a back (like stencil shadow) and don't
  68. * want to clip the back.
  69. */
  70. bool getDepthClipEnable() const { return mData.depthClipEnable; }
  71. /**
  72. * Scissor rectangle allows you to cull all pixels outside of the scissor rectangle.
  73. *
  74. * @see RenderAPICore::setScissorRect
  75. */
  76. bool getScissorEnable() const { return mData.scissorEnable; }
  77. /**
  78. * Determines how are samples in multi-sample render targets handled. If disabled all samples in the render target
  79. * will be written the same value, and if enabled each sample will be generated separately.
  80. *
  81. * @note In order to get an antialiased image you need to both enable this option and use a MSAA render target.
  82. */
  83. bool getMultisampleEnable() const { return mData.multisampleEnable; }
  84. /**
  85. * Determines should the lines be antialiased. This is separate from multi-sample antialiasing setting as lines can
  86. * be antialiased without multi-sampling.
  87. *
  88. * @note This setting is usually ignored if MSAA is used, as that provides sufficient antialiasing.
  89. */
  90. bool getAntialiasedLineEnable() const { return mData.antialiasedLineEnable; }
  91. /** Returns the hash value generated from the rasterizer state properties. */
  92. UINT64 getHash() const { return mHash; }
  93. protected:
  94. friend class RasterizerState;
  95. friend class RasterizerStateCore;
  96. friend class RasterizerStateRTTI;
  97. RASTERIZER_STATE_DESC mData;
  98. UINT64 mHash;
  99. };
  100. /**
  101. * Render system pipeline state that allows you to modify how an object is rasterized (how are polygons converted
  102. * to pixels).
  103. *
  104. * @note Rasterizer states are immutable. Sim thread only.
  105. */
  106. class BS_CORE_EXPORT RasterizerState : public IReflectable, public CoreObject
  107. {
  108. public:
  109. virtual ~RasterizerState();
  110. /** Returns information about the rasterizer state. */
  111. const RasterizerProperties& getProperties() const;
  112. /** Retrieves a core implementation of the rasterizer state usable only from the core thread. */
  113. SPtr<RasterizerStateCore> getCore() const;
  114. /** Creates a new rasterizer state using the specified rasterizer state descriptor structure. */
  115. static SPtr<RasterizerState> create(const RASTERIZER_STATE_DESC& desc);
  116. /** Returns the default rasterizer state. */
  117. static const SPtr<RasterizerState>& getDefault();
  118. /** Generates a hash value from a rasterizer state descriptor. */
  119. static UINT64 generateHash(const RASTERIZER_STATE_DESC& desc);
  120. protected:
  121. friend class RenderStateManager;
  122. RasterizerState(const RASTERIZER_STATE_DESC& desc);
  123. /** @copydoc CoreObject::createCore */
  124. SPtr<CoreObjectCore> createCore() const override;
  125. RasterizerProperties mProperties;
  126. mutable UINT32 mId;
  127. /************************************************************************/
  128. /* RTTI */
  129. /************************************************************************/
  130. public:
  131. friend class RasterizerStateRTTI;
  132. static RTTITypeBase* getRTTIStatic();
  133. virtual RTTITypeBase* getRTTI() const override;
  134. };
  135. /** @} */
  136. /** @addtogroup RenderAPI-Internal
  137. * @{
  138. */
  139. /**
  140. * Core thread version of RasterizerState.
  141. *
  142. * @note Core thread.
  143. */
  144. class BS_CORE_EXPORT RasterizerStateCore : public CoreObjectCore
  145. {
  146. public:
  147. virtual ~RasterizerStateCore();
  148. /** Returns information about the rasterizer state. */
  149. const RasterizerProperties& getProperties() const;
  150. /** Returns a unique state ID. Only the lowest 10 bits are used. */
  151. UINT32 getId() const { return mId; }
  152. /** Creates a new rasterizer state using the specified rasterizer state descriptor structure. */
  153. static SPtr<RasterizerStateCore> create(const RASTERIZER_STATE_DESC& desc);
  154. /** Returns the default rasterizer state. */
  155. static const SPtr<RasterizerStateCore>& getDefault();
  156. protected:
  157. friend class RenderStateCoreManager;
  158. RasterizerStateCore(const RASTERIZER_STATE_DESC& desc, UINT32 id);
  159. /** @copydoc CoreObjectCore::initialize */
  160. void initialize() override;
  161. /** Creates any API-specific state objects. */
  162. virtual void createInternal() { }
  163. RasterizerProperties mProperties;
  164. UINT32 mId;
  165. };
  166. /** @} */
  167. }
  168. /** @cond STDLIB */
  169. /** @addtogroup RenderAPI
  170. * @{
  171. */
  172. namespace std
  173. {
  174. /** Hash value generator for RASTERIZER_STATE_DESC. */
  175. template<>
  176. struct hash<BansheeEngine::RASTERIZER_STATE_DESC>
  177. {
  178. size_t operator()(const BansheeEngine::RASTERIZER_STATE_DESC& value) const
  179. {
  180. return (size_t)BansheeEngine::RasterizerState::generateHash(value);
  181. }
  182. };
  183. }
  184. /** @} */
  185. /** @endcond */