BsBlendState.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup RenderAPI
  7. * @{
  8. */
  9. /**
  10. * Structure that describes blend states for a single render target. Used internally by BLEND_STATE_DESC for
  11. * initializing a BlendState.
  12. *
  13. * @see BLEND_STATE_DESC
  14. * @see BlendState
  15. */
  16. struct BS_CORE_EXPORT RENDER_TARGET_BLEND_STATE_DESC
  17. {
  18. RENDER_TARGET_BLEND_STATE_DESC()
  19. : blendEnable(false)
  20. , srcBlend(BF_ONE)
  21. , dstBlend(BF_ZERO)
  22. , blendOp(BO_ADD)
  23. , srcBlendAlpha(BF_ONE)
  24. , dstBlendAlpha(BF_ZERO)
  25. , blendOpAlpha(BO_ADD)
  26. , renderTargetWriteMask(0xFF)
  27. { }
  28. bool operator==(const RENDER_TARGET_BLEND_STATE_DESC& rhs) const;
  29. bool blendEnable;
  30. BlendFactor srcBlend;
  31. BlendFactor dstBlend;
  32. BlendOperation blendOp;
  33. BlendFactor srcBlendAlpha;
  34. BlendFactor dstBlendAlpha;
  35. BlendOperation blendOpAlpha;
  36. // Enable write to RGBA channels separately by setting first four bits (0 - R, 1 - G, 2 - B, 3 - A)
  37. UINT8 renderTargetWriteMask;
  38. };
  39. /** Structure that describes render pipeline blend states. Used for initializing BlendState. */
  40. struct BS_CORE_EXPORT BLEND_STATE_DESC
  41. {
  42. BLEND_STATE_DESC()
  43. : alphaToCoverageEnable(false)
  44. , independantBlendEnable(false)
  45. { }
  46. bool operator==(const BLEND_STATE_DESC& rhs) const;
  47. bool alphaToCoverageEnable;
  48. bool independantBlendEnable;
  49. RENDER_TARGET_BLEND_STATE_DESC renderTargetDesc[BS_MAX_MULTIPLE_RENDER_TARGETS];
  50. };
  51. /** Properties of a BlendState. Shared between sim and core thread versions of BlendState. */
  52. class BS_CORE_EXPORT BlendProperties
  53. {
  54. public:
  55. BlendProperties(const BLEND_STATE_DESC& desc);
  56. /**
  57. * Alpha to coverage allows you to perform blending without needing to worry about order of rendering like regular
  58. * blending does. It requires multi-sampling to be active in order to work, and you need to supply an alpha texture
  59. * that determines object transparency.
  60. *
  61. * Blending is then performed by only using sub-samples covered by the alpha texture for the current pixel and
  62. * combining them with sub-samples previously stored.
  63. *
  64. * Be aware this is a limited technique only useful for certain situations. Unless you are having performance
  65. * problems use regular blending.
  66. */
  67. bool getAlphaToCoverageEnabled() const { return mData.alphaToCoverageEnable; }
  68. /**
  69. * When not set, only the first render target blend descriptor will be used for all render targets. If set each
  70. * render target will use its own blend descriptor.
  71. */
  72. bool getIndependantBlendEnable() const { return mData.independantBlendEnable; }
  73. /**
  74. * Queries is blending enabled for the specified render target. Blending allows you to combine the color from
  75. * current and previous pixel based on some value.
  76. */
  77. bool getBlendEnabled(UINT32 renderTargetIdx) const;
  78. /**
  79. * Determines what should the source blend factor be. This value determines what will the color being generated
  80. * currently be multiplied by.
  81. */
  82. BlendFactor getSrcBlend(UINT32 renderTargetIdx) const;
  83. /**
  84. * Determines what should the destination blend factor be. This value determines what will the color already in
  85. * render target be multiplied by.
  86. */
  87. BlendFactor getDstBlend(UINT32 renderTargetIdx) const;
  88. /**
  89. * Determines how are source and destination colors combined (after they are multiplied by their respective blend
  90. * factors).
  91. */
  92. BlendOperation getBlendOperation(UINT32 renderTargetIdx) const;
  93. /**
  94. * Determines what should the alpha source blend factor be. This value determines what will the alpha value being
  95. * generated currently be multiplied by.
  96. */
  97. BlendFactor getAlphaSrcBlend(UINT32 renderTargetIdx) const;
  98. /**
  99. * Determines what should the alpha destination blend factor be. This value determines what will the alpha value
  100. * already in render target be multiplied by.
  101. */
  102. BlendFactor getAlphaDstBlend(UINT32 renderTargetIdx) const;
  103. /**
  104. * Determines how are source and destination alpha values combined (after they are multiplied by their respective
  105. * blend factors).
  106. */
  107. BlendOperation getAlphaBlendOperation(UINT32 renderTargetIdx) const;
  108. /**
  109. * Render target write mask allows to choose which pixel components should the pixel shader output.
  110. *
  111. * Only the first four bits are used. First bit representing red, second green, third blue and fourth alpha value.
  112. * Set bits means pixel shader will output those channels.
  113. */
  114. UINT8 getRenderTargetWriteMask(UINT32 renderTargetIdx) const;
  115. /** Returns the hash value generated from the blend state properties. */
  116. UINT64 getHash() const { return mHash; }
  117. protected:
  118. friend class BlendState;
  119. friend class BlendStateCore;
  120. friend class BlendStateRTTI;
  121. BLEND_STATE_DESC mData;
  122. UINT64 mHash;
  123. };
  124. /** @cond INTERNAL */
  125. /**
  126. * Core thread version of BlendState.
  127. *
  128. * @note Core thread.
  129. */
  130. class BS_CORE_EXPORT BlendStateCore : public CoreObjectCore
  131. {
  132. public:
  133. virtual ~BlendStateCore();
  134. /** Returns information about the blend state. */
  135. const BlendProperties& getProperties() const;
  136. /** Returns a unique state ID. Only the lowest 10 bits are used. */
  137. UINT32 getId() const { return mId; }
  138. /** Returns the default blend state that you may use when no other is available. */
  139. static const SPtr<BlendStateCore>& getDefault();
  140. protected:
  141. friend class RenderStateCoreManager;
  142. BlendStateCore(const BLEND_STATE_DESC& desc, UINT32 id);
  143. /** @copydoc CoreObjectCore::initialize */
  144. void initialize() override;
  145. /** Creates any API-specific state objects. */
  146. virtual void createInternal() { }
  147. BlendProperties mProperties;
  148. UINT32 mId;
  149. };
  150. /** @endcond */
  151. /**
  152. * Render system pipeline state that allows you to modify how an object is rendered. More exactly this state allows to
  153. * you to control how is a rendered object blended with any previously renderer objects.
  154. *
  155. * @note Blend states are immutable. Sim thread only.
  156. */
  157. class BS_CORE_EXPORT BlendState : public IReflectable, public CoreObject
  158. {
  159. public:
  160. virtual ~BlendState();
  161. /** Returns information about a blend state. */
  162. const BlendProperties& getProperties() const;
  163. /** Retrieves a core implementation of the sampler state usable only from the core thread. */
  164. SPtr<BlendStateCore> getCore() const;
  165. /** Creates a new blend state using the specified blend state description structure. */
  166. static BlendStatePtr create(const BLEND_STATE_DESC& desc);
  167. /** Returns the default blend state that you may use when no other is available. */
  168. static const BlendStatePtr& getDefault();
  169. /** Generates a hash value from a blend state descriptor. */
  170. static UINT64 generateHash(const BLEND_STATE_DESC& desc);
  171. protected:
  172. friend class RenderStateManager;
  173. BlendState(const BLEND_STATE_DESC& desc);
  174. /** @copydoc CoreObjectCore::createCore */
  175. SPtr<CoreObjectCore> createCore() const override;
  176. BlendProperties mProperties;
  177. mutable UINT32 mId;
  178. /************************************************************************/
  179. /* RTTI */
  180. /************************************************************************/
  181. public:
  182. friend class BlendStateRTTI;
  183. static RTTITypeBase* getRTTIStatic();
  184. virtual RTTITypeBase* getRTTI() const override;
  185. };
  186. /** @} */
  187. }
  188. /** @cond STDLIB */
  189. /** @addtogroup RenderAPI
  190. * @{
  191. */
  192. /** Hash value generator for BLEND_STATE_DESC. */
  193. template<>
  194. struct std::hash<BansheeEngine::BLEND_STATE_DESC>
  195. {
  196. size_t operator()(const BansheeEngine::BLEND_STATE_DESC& value) const
  197. {
  198. return (size_t)BansheeEngine::BlendState::generateHash(value);
  199. }
  200. };
  201. /** @} */
  202. /** @endcond */