BsDepthStencilState.h 9.4 KB

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