BsDepthStencilState.h 8.8 KB

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