BsBlendState.h 8.2 KB

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