BsRasterizerState.h 7.7 KB

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