BsBlendState.h 7.7 KB

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