2
0

BsBlendState.h 8.4 KB

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