BsSamplerState.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "BsSamplerState.h"
  2. #include "BsSamplerStateRTTI.h"
  3. #include "BsRenderStateManager.h"
  4. #include "BsRenderAPI.h"
  5. #include "BsException.h"
  6. #include "BsResources.h"
  7. namespace BansheeEngine
  8. {
  9. bool SAMPLER_STATE_DESC::operator == (const SAMPLER_STATE_DESC& rhs) const
  10. {
  11. return addressMode == rhs.addressMode &&
  12. minFilter == rhs.minFilter &&
  13. magFilter == rhs.magFilter &&
  14. mipFilter == rhs.mipFilter &&
  15. maxAniso == rhs.maxAniso &&
  16. mipmapBias == rhs.mipmapBias &&
  17. mipMin == rhs.mipMin &&
  18. mipMax == rhs.mipMax &&
  19. borderColor == rhs.borderColor &&
  20. comparisonFunc == rhs.comparisonFunc;
  21. }
  22. SamplerProperties::SamplerProperties(const SAMPLER_STATE_DESC& desc)
  23. :mData(desc), mHash(SamplerState::generateHash(desc))
  24. { }
  25. FilterOptions SamplerProperties::getTextureFiltering(FilterType ft) const
  26. {
  27. switch (ft)
  28. {
  29. case FT_MIN:
  30. return mData.minFilter;
  31. case FT_MAG:
  32. return mData.magFilter;
  33. case FT_MIP:
  34. return mData.mipFilter;
  35. }
  36. return mData.minFilter;
  37. }
  38. const Color& SamplerProperties::getBorderColor() const
  39. {
  40. return mData.borderColor;
  41. }
  42. SamplerStateCore::SamplerStateCore(const SAMPLER_STATE_DESC& desc)
  43. :mProperties(desc)
  44. {
  45. }
  46. SamplerStateCore::~SamplerStateCore()
  47. {
  48. RenderStateCoreManager::instance().notifySamplerStateDestroyed(mProperties.mData);
  49. }
  50. void SamplerStateCore::initialize()
  51. {
  52. // Since we cache states it's possible this object was already initialized
  53. // (i.e. multiple sim-states can share a single core-state)
  54. if (isInitialized())
  55. return;
  56. createInternal();
  57. CoreObjectCore::initialize();
  58. }
  59. const SamplerProperties& SamplerStateCore::getProperties() const
  60. {
  61. return mProperties;
  62. }
  63. const SPtr<SamplerStateCore>& SamplerStateCore::getDefault()
  64. {
  65. return RenderStateCoreManager::instance().getDefaultSamplerState();
  66. }
  67. SamplerState::SamplerState(const SAMPLER_STATE_DESC& desc)
  68. :mProperties(desc)
  69. {
  70. }
  71. SamplerState::~SamplerState()
  72. {
  73. }
  74. SPtr<SamplerStateCore> SamplerState::getCore() const
  75. {
  76. return std::static_pointer_cast<SamplerStateCore>(mCoreSpecific);
  77. }
  78. SPtr<CoreObjectCore> SamplerState::createCore() const
  79. {
  80. return RenderStateCoreManager::instance()._createSamplerState(mProperties.mData);
  81. }
  82. SamplerStatePtr SamplerState::create(const SAMPLER_STATE_DESC& desc)
  83. {
  84. return RenderStateManager::instance().createSamplerState(desc);
  85. }
  86. const SamplerStatePtr& SamplerState::getDefault()
  87. {
  88. return RenderStateManager::instance().getDefaultSamplerState();
  89. }
  90. UINT64 SamplerState::generateHash(const SAMPLER_STATE_DESC& desc)
  91. {
  92. size_t hash = 0;
  93. hash_combine(hash, (UINT32)desc.addressMode.u);
  94. hash_combine(hash, (UINT32)desc.addressMode.v);
  95. hash_combine(hash, (UINT32)desc.addressMode.w);
  96. hash_combine(hash, (UINT32)desc.minFilter);
  97. hash_combine(hash, (UINT32)desc.magFilter);
  98. hash_combine(hash, (UINT32)desc.mipFilter);
  99. hash_combine(hash, desc.maxAniso);
  100. hash_combine(hash, desc.mipmapBias);
  101. hash_combine(hash, desc.mipMin);
  102. hash_combine(hash, desc.mipMax);
  103. hash_combine(hash, desc.borderColor);
  104. hash_combine(hash, (UINT32)desc.comparisonFunc);
  105. return (UINT64)hash;
  106. }
  107. const SamplerProperties& SamplerState::getProperties() const
  108. {
  109. return mProperties;
  110. }
  111. /************************************************************************/
  112. /* RTTI */
  113. /************************************************************************/
  114. RTTITypeBase* SamplerState::getRTTIStatic()
  115. {
  116. return SamplerStateRTTI::instance();
  117. }
  118. RTTITypeBase* SamplerState::getRTTI() const
  119. {
  120. return SamplerState::getRTTIStatic();
  121. }
  122. }