BsBlendState.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "RenderAPI/BsBlendState.h"
  4. #include "Managers/BsRenderStateManager.h"
  5. #include "RenderAPI/BsRenderAPI.h"
  6. #include "RTTI/BsBlendStateRTTI.h"
  7. #include "Resources/BsResources.h"
  8. namespace bs
  9. {
  10. bool RENDER_TARGET_BLEND_STATE_DESC::operator == (const RENDER_TARGET_BLEND_STATE_DESC& rhs) const
  11. {
  12. return blendEnable == rhs.blendEnable &&
  13. srcBlend == rhs.srcBlend &&
  14. dstBlend == rhs.dstBlend &&
  15. blendOp == rhs.blendOp &&
  16. srcBlendAlpha == rhs.srcBlendAlpha &&
  17. dstBlendAlpha == rhs.dstBlendAlpha &&
  18. blendOpAlpha == rhs.blendOpAlpha &&
  19. renderTargetWriteMask == rhs.renderTargetWriteMask;
  20. }
  21. bool BLEND_STATE_DESC::operator == (const BLEND_STATE_DESC& rhs) const
  22. {
  23. bool equals = alphaToCoverageEnable == rhs.alphaToCoverageEnable &&
  24. independantBlendEnable == rhs.independantBlendEnable;
  25. if (equals)
  26. {
  27. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  28. {
  29. equals &= renderTargetDesc[i] == rhs.renderTargetDesc[i];
  30. }
  31. }
  32. return equals;
  33. }
  34. BlendProperties::BlendProperties(const BLEND_STATE_DESC& desc)
  35. :mData(desc), mHash(BlendState::generateHash(desc))
  36. { }
  37. bool BlendProperties::getBlendEnabled(UINT32 renderTargetIdx) const
  38. {
  39. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  40. return mData.renderTargetDesc[renderTargetIdx].blendEnable;
  41. }
  42. BlendFactor BlendProperties::getSrcBlend(UINT32 renderTargetIdx) const
  43. {
  44. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  45. return mData.renderTargetDesc[renderTargetIdx].srcBlend;
  46. }
  47. BlendFactor BlendProperties::getDstBlend(UINT32 renderTargetIdx) const
  48. {
  49. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  50. return mData.renderTargetDesc[renderTargetIdx].dstBlend;
  51. }
  52. BlendOperation BlendProperties::getBlendOperation(UINT32 renderTargetIdx) const
  53. {
  54. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  55. return mData.renderTargetDesc[renderTargetIdx].blendOp;
  56. }
  57. BlendFactor BlendProperties::getAlphaSrcBlend(UINT32 renderTargetIdx) const
  58. {
  59. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  60. return mData.renderTargetDesc[renderTargetIdx].srcBlendAlpha;
  61. }
  62. BlendFactor BlendProperties::getAlphaDstBlend(UINT32 renderTargetIdx) const
  63. {
  64. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  65. return mData.renderTargetDesc[renderTargetIdx].dstBlendAlpha;
  66. }
  67. BlendOperation BlendProperties::getAlphaBlendOperation(UINT32 renderTargetIdx) const
  68. {
  69. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  70. return mData.renderTargetDesc[renderTargetIdx].blendOpAlpha;
  71. }
  72. UINT8 BlendProperties::getRenderTargetWriteMask(UINT32 renderTargetIdx) const
  73. {
  74. assert(renderTargetIdx >= 0 && renderTargetIdx < BS_MAX_MULTIPLE_RENDER_TARGETS);
  75. return mData.renderTargetDesc[renderTargetIdx].renderTargetWriteMask;
  76. }
  77. BlendState::BlendState(const BLEND_STATE_DESC& desc)
  78. :mProperties(desc), mId(0)
  79. { }
  80. BlendState::~BlendState()
  81. {
  82. }
  83. SPtr<ct::BlendState> BlendState::getCore() const
  84. {
  85. return std::static_pointer_cast<ct::BlendState>(mCoreSpecific);
  86. }
  87. SPtr<ct::CoreObject> BlendState::createCore() const
  88. {
  89. SPtr<ct::BlendState> core = ct::RenderStateManager::instance()._createBlendState(mProperties.mData);
  90. mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable
  91. return core;
  92. }
  93. const BlendProperties& BlendState::getProperties() const
  94. {
  95. return mProperties;
  96. }
  97. const SPtr<BlendState>& BlendState::getDefault()
  98. {
  99. return RenderStateManager::instance().getDefaultBlendState();
  100. }
  101. SPtr<BlendState> BlendState::create(const BLEND_STATE_DESC& desc)
  102. {
  103. return RenderStateManager::instance().createBlendState(desc);
  104. }
  105. UINT64 BlendState::generateHash(const BLEND_STATE_DESC& desc)
  106. {
  107. size_t hash = 0;
  108. hash_combine(hash, desc.alphaToCoverageEnable);
  109. hash_combine(hash, desc.independantBlendEnable);
  110. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  111. {
  112. hash_combine(hash, desc.renderTargetDesc[i].blendEnable);
  113. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].srcBlend);
  114. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].dstBlend);
  115. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].blendOp);
  116. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].srcBlendAlpha);
  117. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].dstBlendAlpha);
  118. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].blendOpAlpha);
  119. hash_combine(hash, desc.renderTargetDesc[i].renderTargetWriteMask);
  120. }
  121. return (UINT64)hash;
  122. }
  123. /************************************************************************/
  124. /* RTTI */
  125. /************************************************************************/
  126. RTTITypeBase* BlendState::getRTTIStatic()
  127. {
  128. return BlendStateRTTI::instance();
  129. }
  130. RTTITypeBase* BlendState::getRTTI() const
  131. {
  132. return BlendState::getRTTIStatic();
  133. }
  134. namespace ct
  135. {
  136. BlendState::BlendState(const BLEND_STATE_DESC& desc, UINT32 id)
  137. :mProperties(desc), mId(id)
  138. {
  139. }
  140. BlendState::~BlendState()
  141. {
  142. }
  143. void BlendState::initialize()
  144. {
  145. // Since we cache states it's possible this object was already initialized
  146. // (i.e. multiple sim-states can share a single core-state)
  147. if (isInitialized())
  148. return;
  149. createInternal();
  150. CoreObject::initialize();
  151. }
  152. const BlendProperties& BlendState::getProperties() const
  153. {
  154. return mProperties;
  155. }
  156. SPtr<BlendState> BlendState::create(const BLEND_STATE_DESC& desc)
  157. {
  158. return RenderStateManager::instance().createBlendState(desc);
  159. }
  160. const SPtr<BlendState>& BlendState::getDefault()
  161. {
  162. return RenderStateManager::instance().getDefaultBlendState();
  163. }
  164. }
  165. }