BsDepthStencilState.cpp 4.5 KB

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