BsRasterizerState.h 7.0 KB

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