BsSamplerState.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsColor.h"
  4. #include "BsIReflectable.h"
  5. #include "BsCoreObject.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup RenderAPI
  9. * @{
  10. */
  11. /**
  12. * Structure used for initializing a SamplerState.
  13. *
  14. * @see SamplerState
  15. */
  16. struct BS_CORE_EXPORT SAMPLER_STATE_DESC
  17. {
  18. SAMPLER_STATE_DESC()
  19. : minFilter(FO_LINEAR), magFilter(FO_LINEAR), mipFilter(FO_POINT),
  20. maxAniso(0), mipmapBias(0), comparisonFunc(CMPF_ALWAYS_FAIL), mipMin(-FLT_MAX),
  21. mipMax(FLT_MAX), borderColor(Color::White)
  22. { }
  23. bool operator==(const SAMPLER_STATE_DESC& rhs) const;
  24. UVWAddressingMode addressMode;
  25. FilterOptions minFilter;
  26. FilterOptions magFilter;
  27. FilterOptions mipFilter;
  28. UINT32 maxAniso;
  29. float mipmapBias;
  30. float mipMin;
  31. float mipMax;
  32. Color borderColor;
  33. CompareFunction comparisonFunc;
  34. };
  35. /** Properties of SamplerState. Shared between sim and core thread versions of SamplerState. */
  36. class BS_CORE_EXPORT SamplerProperties
  37. {
  38. public:
  39. SamplerProperties(const SAMPLER_STATE_DESC& desc);
  40. /**
  41. * Returns texture addressing mode for each possible texture coordinate. Addressing modes determine how are texture
  42. * coordinates outside of [0, 1] range handled.
  43. */
  44. const UVWAddressingMode& getTextureAddressingMode() const { return mData.addressMode; }
  45. /** Gets the filtering used when sampling from a texture. */
  46. FilterOptions getTextureFiltering(FilterType ftpye) const;
  47. /**
  48. * Gets the anisotropy level. Higher anisotropy means better filtering for textures displayed on an angled slope
  49. * relative to the viewer.
  50. */
  51. unsigned int getTextureAnisotropy() const { return mData.maxAniso; }
  52. /** Gets a function that compares sampled data with existing sampled data. */
  53. CompareFunction getComparisonFunction() const { return mData.comparisonFunc; }
  54. /**
  55. * Mipmap bias allows you to adjust the mipmap selection calculation. Negative values force a larger mipmap to be
  56. * used, and positive values smaller. Units are in values of mip levels, so -1 means use a mipmap one level higher
  57. * than default.
  58. */
  59. float getTextureMipmapBias() const { return mData.mipmapBias; }
  60. /** Returns the minimum mip map level. */
  61. float getMinimumMip() const { return mData.mipMin; }
  62. /** Returns the maximum mip map level. */
  63. float getMaximumMip() const { return mData.mipMax; }
  64. /**
  65. * Gets the border color that will be used when border texture addressing is used and texture address is outside of
  66. * the valid range.
  67. */
  68. const Color& getBorderColor() const;
  69. /** Returns the hash value generated from the sampler state properties. */
  70. UINT64 getHash() const { return mHash; }
  71. /** Returns the descriptor originally used for creating the sampler state. */
  72. SAMPLER_STATE_DESC getDesc() const { return mData; }
  73. protected:
  74. friend class SamplerState;
  75. friend class SamplerStateCore;
  76. friend class SamplerStateRTTI;
  77. SAMPLER_STATE_DESC mData;
  78. UINT64 mHash;
  79. };
  80. /** @cond INTERNAL */
  81. /**
  82. * Core thread version of SamplerState.
  83. *
  84. * @note Core thread.
  85. */
  86. class BS_CORE_EXPORT SamplerStateCore : public CoreObjectCore
  87. {
  88. public:
  89. virtual ~SamplerStateCore();
  90. /** Returns information about the sampler state. */
  91. const SamplerProperties& getProperties() const;
  92. /** Returns the default sampler state. */
  93. static const SPtr<SamplerStateCore>& getDefault();
  94. protected:
  95. friend class RenderStateCoreManager;
  96. SamplerStateCore(const SAMPLER_STATE_DESC& desc);
  97. /** @copydoc CoreObjectCore::initialize */
  98. void initialize() override;
  99. /** Creates any API-specific state objects. */
  100. virtual void createInternal() { }
  101. SamplerProperties mProperties;
  102. };
  103. /** @endcond */
  104. /**
  105. * Class representing the state of a texture sampler.
  106. *
  107. * @note
  108. * Sampler units are used for retrieving and filtering data from textures set in a GPU program. Sampler states are
  109. * immutable.
  110. * @note
  111. * Sim thread.
  112. */
  113. class BS_CORE_EXPORT SamplerState : public IReflectable, public CoreObject
  114. {
  115. public:
  116. virtual ~SamplerState();
  117. /** Returns information about the sampler state. */
  118. const SamplerProperties& getProperties() const;
  119. /** Retrieves a core implementation of the sampler state usable only from the core thread. */
  120. SPtr<SamplerStateCore> getCore() const;
  121. /** Creates a new sampler state using the provided descriptor structure. */
  122. static SamplerStatePtr create(const SAMPLER_STATE_DESC& desc);
  123. /** Returns the default sampler state. */
  124. static const SamplerStatePtr& getDefault();
  125. /** Generates a hash value from a sampler state descriptor. */
  126. static UINT64 generateHash(const SAMPLER_STATE_DESC& desc);
  127. protected:
  128. SamplerState(const SAMPLER_STATE_DESC& desc);
  129. /** @copydoc CoreObjectCore::createCore */
  130. SPtr<CoreObjectCore> createCore() const override;
  131. SamplerProperties mProperties;
  132. friend class RenderStateManager;
  133. /************************************************************************/
  134. /* RTTI */
  135. /************************************************************************/
  136. public:
  137. friend class SamplerStateRTTI;
  138. static RTTITypeBase* getRTTIStatic();
  139. virtual RTTITypeBase* getRTTI() const override;
  140. };
  141. /** @} */
  142. }
  143. /** @cond STDLIB */
  144. /** @addtogroup RenderAPI
  145. * @{
  146. */
  147. /** Hash value generator for SAMPLER_STATE_DESC. */
  148. template<>
  149. struct std::hash<BansheeEngine::SAMPLER_STATE_DESC>
  150. {
  151. size_t operator()(const BansheeEngine::SAMPLER_STATE_DESC& value) const
  152. {
  153. return (size_t)BansheeEngine::SamplerState::generateHash(value);
  154. }
  155. };
  156. /** @} */
  157. /** @endcond */