BsBlendState.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsBlendState.h"
  4. #include "BsRenderStateManager.h"
  5. #include "BsRenderAPI.h"
  6. #include "BsBlendStateRTTI.h"
  7. #include "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. BlendStateCore::BlendStateCore(const BLEND_STATE_DESC& desc, UINT32 id)
  78. :mProperties(desc), mId(id)
  79. {
  80. }
  81. BlendStateCore::~BlendStateCore()
  82. {
  83. }
  84. void BlendStateCore::initialize()
  85. {
  86. // Since we cache states it's possible this object was already initialized
  87. // (i.e. multiple sim-states can share a single core-state)
  88. if (isInitialized())
  89. return;
  90. createInternal();
  91. CoreObjectCore::initialize();
  92. }
  93. const BlendProperties& BlendStateCore::getProperties() const
  94. {
  95. return mProperties;
  96. }
  97. SPtr<BlendStateCore> BlendStateCore::create(const BLEND_STATE_DESC& desc)
  98. {
  99. return RenderStateCoreManager::instance().createBlendState(desc);
  100. }
  101. const SPtr<BlendStateCore>& BlendStateCore::getDefault()
  102. {
  103. return RenderStateCoreManager::instance().getDefaultBlendState();
  104. }
  105. BlendState::BlendState(const BLEND_STATE_DESC& desc)
  106. :mProperties(desc), mId(0)
  107. { }
  108. BlendState::~BlendState()
  109. {
  110. }
  111. SPtr<BlendStateCore> BlendState::getCore() const
  112. {
  113. return std::static_pointer_cast<BlendStateCore>(mCoreSpecific);
  114. }
  115. SPtr<CoreObjectCore> BlendState::createCore() const
  116. {
  117. SPtr<BlendStateCore> core = RenderStateCoreManager::instance()._createBlendState(mProperties.mData);
  118. mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable
  119. return core;
  120. }
  121. const BlendProperties& BlendState::getProperties() const
  122. {
  123. return mProperties;
  124. }
  125. const SPtr<BlendState>& BlendState::getDefault()
  126. {
  127. return RenderStateManager::instance().getDefaultBlendState();
  128. }
  129. SPtr<BlendState> BlendState::create(const BLEND_STATE_DESC& desc)
  130. {
  131. return RenderStateManager::instance().createBlendState(desc);
  132. }
  133. UINT64 BlendState::generateHash(const BLEND_STATE_DESC& desc)
  134. {
  135. size_t hash = 0;
  136. hash_combine(hash, desc.alphaToCoverageEnable);
  137. hash_combine(hash, desc.independantBlendEnable);
  138. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  139. {
  140. hash_combine(hash, desc.renderTargetDesc[i].blendEnable);
  141. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].srcBlend);
  142. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].dstBlend);
  143. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].blendOp);
  144. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].srcBlendAlpha);
  145. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].dstBlendAlpha);
  146. hash_combine(hash, (UINT32)desc.renderTargetDesc[i].blendOpAlpha);
  147. hash_combine(hash, desc.renderTargetDesc[i].renderTargetWriteMask);
  148. }
  149. return (UINT64)hash;
  150. }
  151. /************************************************************************/
  152. /* RTTI */
  153. /************************************************************************/
  154. RTTITypeBase* BlendState::getRTTIStatic()
  155. {
  156. return BlendStateRTTI::instance();
  157. }
  158. RTTITypeBase* BlendState::getRTTI() const
  159. {
  160. return BlendState::getRTTIStatic();
  161. }
  162. }