BsDepthStencilState.h 8.3 KB

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