BsSamplerState.h 5.3 KB

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