BsDepthStencilState.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsCoreObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Descriptor structured used for initializing DepthStencilState.
  9. *
  10. * @see DepthStencilState
  11. */
  12. struct BS_CORE_EXPORT DEPTH_STENCIL_STATE_DESC
  13. {
  14. DEPTH_STENCIL_STATE_DESC()
  15. : depthReadEnable(true)
  16. , depthWriteEnable(true)
  17. , depthComparisonFunc(CMPF_LESS)
  18. , stencilEnable(false)
  19. , stencilReadMask(0xFF)
  20. , stencilWriteMask(0xFF)
  21. , frontStencilFailOp(SOP_KEEP)
  22. , frontStencilZFailOp(SOP_KEEP)
  23. , frontStencilPassOp(SOP_KEEP)
  24. , frontStencilComparisonFunc(CMPF_ALWAYS_PASS)
  25. , backStencilFailOp(SOP_KEEP)
  26. , backStencilZFailOp(SOP_KEEP)
  27. , backStencilPassOp(SOP_KEEP)
  28. , backStencilComparisonFunc(CMPF_ALWAYS_PASS)
  29. { }
  30. bool operator==(const DEPTH_STENCIL_STATE_DESC& rhs) const;
  31. bool depthReadEnable;
  32. bool depthWriteEnable;
  33. CompareFunction depthComparisonFunc;
  34. bool stencilEnable;
  35. UINT8 stencilReadMask;
  36. UINT8 stencilWriteMask;
  37. StencilOperation frontStencilFailOp;
  38. StencilOperation frontStencilZFailOp;
  39. StencilOperation frontStencilPassOp;
  40. CompareFunction frontStencilComparisonFunc;
  41. StencilOperation backStencilFailOp;
  42. StencilOperation backStencilZFailOp;
  43. StencilOperation backStencilPassOp;
  44. CompareFunction backStencilComparisonFunc;
  45. };
  46. BS_ALLOW_MEMCPY_SERIALIZATION(DEPTH_STENCIL_STATE_DESC);
  47. /**
  48. * @brief Information about a depth stencil state.
  49. */
  50. class BS_CORE_EXPORT DepthStencilProperties
  51. {
  52. public:
  53. DepthStencilProperties(const DEPTH_STENCIL_STATE_DESC& desc);
  54. /**
  55. * @brief If enabled, any pixel about to be written will be tested against the depth value
  56. * currently in the buffer. If the depth test passes (depending on the set value
  57. * and chosen depth comparison function), that pixel is written and depth is
  58. * updated (if depth write is enabled).
  59. */
  60. bool getDepthReadEnable() const { return mData.depthReadEnable; }
  61. /**
  62. * @brief If enabled rendering pixels will update the depth buffer value.
  63. */
  64. bool getDepthWriteEnable() const { return mData.depthWriteEnable; }
  65. /**
  66. * @brief Determines what operation should the renderer use when comparing previous and
  67. * current depth value. If the operation passes, pixel with the current depth
  68. * value will be considered visible.
  69. */
  70. CompareFunction getDepthComparisonFunc() const { return mData.depthComparisonFunc; }
  71. /**
  72. * @brief If true then stencil buffer will also be updated when a pixel is written, and
  73. * pixels will be tested against the stencil buffer before rendering.
  74. */
  75. bool getStencilEnable() const { return mData.stencilEnable; }
  76. /**
  77. * @brief Mask to apply to any value read from the stencil buffer, before applying the
  78. * stencil comparison function.
  79. */
  80. UINT8 getStencilReadMask() const { return mData.stencilReadMask; }
  81. /**
  82. * @brief Mask to apply to any value about to be written in the stencil buffer.
  83. */
  84. UINT8 getStencilWriteMask() const { return mData.stencilWriteMask; }
  85. /**
  86. * @brief Operation that happens when stencil comparison function fails on a front facing polygon.
  87. */
  88. StencilOperation getStencilFrontFailOp() const { return mData.frontStencilFailOp; }
  89. /**
  90. * @brief Operation that happens when stencil comparison function passes but depth test fails
  91. * on a front facing polygon.
  92. */
  93. StencilOperation getStencilFrontZFailOp() const { return mData.frontStencilZFailOp; }
  94. /**
  95. * @brief Operation that happens when stencil comparison function passes on a front facing polygon.
  96. */
  97. StencilOperation getStencilFrontPassOp() const { return mData.frontStencilPassOp; }
  98. /**
  99. * @brief Stencil comparison function used for front facing polygons. Stencil buffer will be modified according
  100. * to previously set stencil operations depending whether this comparison passes or fails.
  101. */
  102. CompareFunction getStencilFrontCompFunc() const { return mData.frontStencilComparisonFunc; }
  103. /**
  104. * @brief Operation that happens when stencil comparison function fails on a back facing polygon.
  105. */
  106. StencilOperation getStencilBackFailOp() const { return mData.backStencilFailOp; }
  107. /**
  108. * @brief Operation that happens when stencil comparison function passes but depth test fails
  109. * on a back facing polygon.
  110. */
  111. StencilOperation getStencilBackZFailOp() const { return mData.backStencilZFailOp; }
  112. /**
  113. * @brief Operation that happens when stencil comparison function passes on a back facing polygon.
  114. */
  115. StencilOperation getStencilBackPassOp() const { return mData.backStencilPassOp; }
  116. /**
  117. * @brief Stencil comparison function used for back facing polygons. Stencil buffer will be modified according
  118. * to previously set stencil operations depending whether this comparison passes or fails.
  119. */
  120. CompareFunction getStencilBackCompFunc() const { return mData.backStencilComparisonFunc; }
  121. /**
  122. * @brief Returns the hash value generated from the depth-stencil state properties.
  123. */
  124. UINT64 getHash() const { return mHash; }
  125. protected:
  126. friend class DepthStencilState;
  127. friend class DepthStencilStateCore;
  128. friend class DepthStencilStateRTTI;
  129. DEPTH_STENCIL_STATE_DESC mData;
  130. UINT64 mHash;
  131. };
  132. /**
  133. * @brief Core thread version of the depth stencil state.
  134. *
  135. * @see DepthStencilState
  136. *
  137. * @note Core thread.
  138. */
  139. class BS_CORE_EXPORT DepthStencilStateCore : public CoreObjectCore
  140. {
  141. public:
  142. virtual ~DepthStencilStateCore();
  143. /**
  144. * @brief Returns information about the depth stencil state.
  145. */
  146. const DepthStencilProperties& getProperties() const;
  147. /**
  148. * @brief Returns a unique state ID. Only the lowest 10 bits are used.
  149. */
  150. UINT32 getId() const { return mId; }
  151. /**
  152. * @brief Returns the default depth stencil state that you may use when no other is available.
  153. */
  154. static const SPtr<DepthStencilStateCore>& getDefault();
  155. protected:
  156. friend class RenderStateCoreManager;
  157. DepthStencilStateCore(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id);
  158. /**
  159. * @copydoc CoreObjectCore::initialize
  160. */
  161. void initialize() override;
  162. /**
  163. * @brief Creates any API-specific state objects.
  164. */
  165. virtual void createInternal() { }
  166. DepthStencilProperties mProperties;
  167. UINT32 mId;
  168. };
  169. /**
  170. * @brief Render system pipeline state that allows you to modify how an object is rendered.
  171. * More exactly this state allows to you to control how are depth and stencil buffers
  172. * modified upon rendering.
  173. *
  174. * @note Depth stencil states are immutable. Sim thread only.
  175. */
  176. class BS_CORE_EXPORT DepthStencilState : public IReflectable, public CoreObject
  177. {
  178. public:
  179. virtual ~DepthStencilState();
  180. /**
  181. * @brief Returns information about the depth stencil state.
  182. */
  183. const DepthStencilProperties& getProperties() const;
  184. /**
  185. * @brief Retrieves a core implementation of a sampler state usable only from the
  186. * core thread.
  187. */
  188. SPtr<DepthStencilStateCore> getCore() const;
  189. /**
  190. * @brief Creates a new depth stencil state using the specified depth stencil state description structure.
  191. */
  192. static DepthStencilStatePtr create(const DEPTH_STENCIL_STATE_DESC& desc);
  193. /**
  194. * @brief Returns the default depth stencil state that you may use when no other is available.
  195. */
  196. static const DepthStencilStatePtr& getDefault();
  197. /**
  198. * @brief Generates a hash value from a depth-stencil state descriptor.
  199. */
  200. static UINT64 generateHash(const DEPTH_STENCIL_STATE_DESC& desc);
  201. protected:
  202. friend class RenderStateManager;
  203. DepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc);
  204. /**
  205. * @copydoc CoreObjectCore::createCore
  206. */
  207. SPtr<CoreObjectCore> createCore() const override;
  208. DepthStencilProperties mProperties;
  209. mutable UINT32 mId;
  210. /************************************************************************/
  211. /* RTTI */
  212. /************************************************************************/
  213. public:
  214. friend class DepthStencilStateRTTI;
  215. static RTTITypeBase* getRTTIStatic();
  216. virtual RTTITypeBase* getRTTI() const override;
  217. };
  218. }
  219. /**
  220. * @brief Hash value generator for DEPTH_STENCIL_STATE_DESC.
  221. */
  222. template<>
  223. struct std::hash<BansheeEngine::DEPTH_STENCIL_STATE_DESC>
  224. {
  225. size_t operator()(const BansheeEngine::DEPTH_STENCIL_STATE_DESC& value) const
  226. {
  227. return (size_t)BansheeEngine::DepthStencilState::generateHash(value);
  228. }
  229. };