BsRasterizerState.h 7.0 KB

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