BsDepthStencilState.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsDepthStencilState.h"
  4. #include "BsRenderStateManager.h"
  5. #include "BsRenderAPI.h"
  6. #include "BsDepthStencilStateRTTI.h"
  7. #include "BsException.h"
  8. #include "BsResources.h"
  9. namespace BansheeEngine
  10. {
  11. bool DEPTH_STENCIL_STATE_DESC::operator == (const DEPTH_STENCIL_STATE_DESC& rhs) const
  12. {
  13. return depthReadEnable == rhs.depthReadEnable &&
  14. depthWriteEnable == rhs.depthWriteEnable &&
  15. depthComparisonFunc == rhs.depthComparisonFunc &&
  16. stencilEnable == rhs.stencilEnable &&
  17. stencilReadMask == rhs.stencilReadMask &&
  18. stencilWriteMask == rhs.stencilWriteMask &&
  19. frontStencilFailOp == rhs.frontStencilFailOp &&
  20. frontStencilZFailOp == rhs.frontStencilZFailOp &&
  21. frontStencilPassOp == rhs.frontStencilPassOp &&
  22. frontStencilComparisonFunc == rhs.frontStencilComparisonFunc &&
  23. backStencilFailOp == rhs.backStencilFailOp &&
  24. backStencilZFailOp == rhs.backStencilZFailOp &&
  25. backStencilPassOp == rhs.backStencilPassOp &&
  26. backStencilComparisonFunc == rhs.backStencilComparisonFunc;
  27. }
  28. DepthStencilProperties::DepthStencilProperties(const DEPTH_STENCIL_STATE_DESC& desc)
  29. :mData(desc), mHash(DepthStencilState::generateHash(desc))
  30. {
  31. }
  32. DepthStencilStateCore::DepthStencilStateCore(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id)
  33. : mProperties(desc), mId(id)
  34. {
  35. }
  36. DepthStencilStateCore::~DepthStencilStateCore()
  37. {
  38. }
  39. void DepthStencilStateCore::initialize()
  40. {
  41. // Since we cache states it's possible this object was already initialized
  42. // (i.e. multiple sim-states can share a single core-state)
  43. if (isInitialized())
  44. return;
  45. createInternal();
  46. CoreObjectCore::initialize();
  47. }
  48. const DepthStencilProperties& DepthStencilStateCore::getProperties() const
  49. {
  50. return mProperties;
  51. }
  52. const SPtr<DepthStencilStateCore>& DepthStencilStateCore::getDefault()
  53. {
  54. return RenderStateCoreManager::instance().getDefaultDepthStencilState();
  55. }
  56. DepthStencilState::DepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc)
  57. :mProperties(desc), mId(0)
  58. {
  59. }
  60. DepthStencilState::~DepthStencilState()
  61. {
  62. }
  63. SPtr<DepthStencilStateCore> DepthStencilState::getCore() const
  64. {
  65. return std::static_pointer_cast<DepthStencilStateCore>(mCoreSpecific);
  66. }
  67. SPtr<CoreObjectCore> DepthStencilState::createCore() const
  68. {
  69. SPtr<DepthStencilStateCore> core = RenderStateCoreManager::instance()._createDepthStencilState(mProperties.mData);
  70. mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable
  71. return core;
  72. }
  73. const DepthStencilStatePtr& DepthStencilState::getDefault()
  74. {
  75. return RenderStateManager::instance().getDefaultDepthStencilState();
  76. }
  77. const DepthStencilProperties& DepthStencilState::getProperties() const
  78. {
  79. return mProperties;
  80. }
  81. DepthStencilStatePtr DepthStencilState::create(const DEPTH_STENCIL_STATE_DESC& desc)
  82. {
  83. return RenderStateManager::instance().createDepthStencilState(desc);
  84. }
  85. UINT64 DepthStencilState::generateHash(const DEPTH_STENCIL_STATE_DESC& desc)
  86. {
  87. size_t hash = 0;
  88. hash_combine(hash, desc.depthReadEnable);
  89. hash_combine(hash, desc.depthWriteEnable);
  90. hash_combine(hash, (UINT32)desc.depthComparisonFunc);
  91. hash_combine(hash, desc.stencilEnable);
  92. hash_combine(hash, desc.stencilReadMask);
  93. hash_combine(hash, desc.stencilWriteMask);
  94. hash_combine(hash, (UINT32)desc.frontStencilFailOp);
  95. hash_combine(hash, (UINT32)desc.frontStencilZFailOp);
  96. hash_combine(hash, (UINT32)desc.frontStencilPassOp);
  97. hash_combine(hash, (UINT32)desc.frontStencilComparisonFunc);
  98. hash_combine(hash, (UINT32)desc.backStencilFailOp);
  99. hash_combine(hash, (UINT32)desc.backStencilZFailOp);
  100. hash_combine(hash, (UINT32)desc.backStencilPassOp);
  101. hash_combine(hash, (UINT32)desc.backStencilComparisonFunc);
  102. return (UINT64)hash;
  103. }
  104. /************************************************************************/
  105. /* RTTI */
  106. /************************************************************************/
  107. RTTITypeBase* DepthStencilState::getRTTIStatic()
  108. {
  109. return DepthStencilStateRTTI::instance();
  110. }
  111. RTTITypeBase* DepthStencilState::getRTTI() const
  112. {
  113. return DepthStencilState::getRTTIStatic();
  114. }
  115. }