CmBlendState.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmResource.h"
  4. #include "CmCommonEnums.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Structure that describes blend states for a single render
  9. * target. Used internally by BLEND_STATE_DESC for initializing a BlendState.
  10. *
  11. * @see BLEND_STATE_DESC
  12. * @see BlendState
  13. */
  14. struct CM_EXPORT RENDER_TARGET_BLEND_STATE_DESC
  15. {
  16. RENDER_TARGET_BLEND_STATE_DESC()
  17. : blendEnable(false)
  18. , srcBlend(BF_ONE)
  19. , dstBlend(BF_ZERO)
  20. , blendOp(BO_ADD)
  21. , srcBlendAlpha(BF_ONE)
  22. , dstBlendAlpha(BF_ZERO)
  23. , blendOpAlpha(BO_ADD)
  24. , renderTargetWriteMask(0xFF)
  25. { }
  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 CM_EXPORT BLEND_STATE_DESC
  43. {
  44. BLEND_STATE_DESC()
  45. : alphaToCoverageEnable(false)
  46. , independantBlendEnable(false)
  47. { }
  48. bool alphaToCoverageEnable;
  49. bool independantBlendEnable;
  50. RENDER_TARGET_BLEND_STATE_DESC renderTargetDesc[CM_MAX_MULTIPLE_RENDER_TARGETS];
  51. };
  52. /**
  53. * @brief Render system pipeline state that allows you to modify how an object is rendered.
  54. * More exactly this state allows to you to control how is a rendered
  55. * object blended with any previously renderer objects.
  56. *
  57. * @note Blend states are immutable. Thread safe.
  58. */
  59. class CM_EXPORT BlendState : public Resource
  60. {
  61. public:
  62. virtual ~BlendState() {}
  63. /**
  64. * @brief Alpha to coverage allows you to perform blending without needing to worry about order of
  65. * rendering like regular blending does. It requires multi-sampling to be active in order to
  66. * work, and you need to supply an alpha texture that determines object transparency.
  67. *
  68. * Blending is then performed by only using sub-samples covered by the alpha texture for the current
  69. * pixel and combining them with sub-samples previously stored.
  70. *
  71. * Be aware this is a limited technique only useful for certain situations. Unless you are having performance
  72. * problems use regular blending.
  73. */
  74. bool getAlphaToCoverageEnabled() const { return mData.alphaToCoverageEnable; }
  75. /**
  76. * @brief When not set, only the first render target blend descriptor will be used for all
  77. * render targets. If set each render target will use its own blend descriptor.
  78. */
  79. bool getIndependantBlendEnable() const { return mData.independantBlendEnable; }
  80. /**
  81. * @brief Queries is blending enabled for the specified render target. Blending
  82. * allows you to combine the color from current and previous pixel based on
  83. * some value.
  84. */
  85. bool getBlendEnabled(UINT32 renderTargetIdx) const;
  86. /**
  87. * @brief Determines what should the source blend factor be. This value determines
  88. * what will the color being generated currently be multiplied by.
  89. */
  90. BlendFactor getSrcBlend(UINT32 renderTargetIdx) const;
  91. /**
  92. * @brief Determines what should the destination blend factor be. This value determines
  93. * what will the color already in render target be multiplied by.
  94. */
  95. BlendFactor getDstBlend(UINT32 renderTargetIdx) const;
  96. /**
  97. * @brief Determines how are source and destination colors combined (after they are multiplied
  98. * by their respective blend factors).
  99. */
  100. BlendOperation getBlendOperation(UINT32 renderTargetIdx) const;
  101. /**
  102. * @brief Determines what should the alpha source blend factor be. This value determines
  103. * what will the alpha value being generated currently be multiplied by.
  104. */
  105. BlendFactor getAlphaSrcBlend(UINT32 renderTargetIdx) const;
  106. /**
  107. * @brief Determines what should the alpha destination blend factor be. This value determines
  108. * what will the alpha value already in render target be multiplied by.
  109. */
  110. BlendFactor getAlphaDstBlend(UINT32 renderTargetIdx) const;
  111. /**
  112. * @brief Determines how are source and destination alpha values combined (after they are multiplied
  113. * by their respective blend factors).
  114. */
  115. BlendOperation getAlphaBlendOperation(UINT32 renderTargetIdx) const;
  116. /**
  117. * @brief Render target write mask allows to choose which pixel components should the pixel shader
  118. * output.
  119. *
  120. * Only the first four bits are used. First bit representing red, second green, third blue and
  121. * fourth alpha value. Set bits means pixel shader will output those channels.
  122. */
  123. UINT8 getRenderTargetWriteMask(UINT32 renderTargetIdx) const;
  124. /**
  125. * @brief Creates a new blend state using the specified blend state description structure.
  126. */
  127. static HBlendState create(const BLEND_STATE_DESC& desc);
  128. /**
  129. * @brief Returns the default blend state that you may use
  130. * when no other is available.
  131. */
  132. static const BlendStatePtr& getDefault();
  133. protected:
  134. friend class RenderStateManager;
  135. /**
  136. * @brief Initializes the blend state. Must be called right after construction.
  137. */
  138. virtual void initialize(const BLEND_STATE_DESC& desc);
  139. BLEND_STATE_DESC mData;
  140. /************************************************************************/
  141. /* RTTI */
  142. /************************************************************************/
  143. public:
  144. friend class BlendStateRTTI;
  145. static RTTITypeBase* getRTTIStatic();
  146. virtual RTTITypeBase* getRTTI() const;
  147. };
  148. }