BsDepthStencilState.cpp 4.1 KB

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