BsSamplerState.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /**
  84. * @brief Returns the descriptor originally used for creating the sampler state.
  85. */
  86. SAMPLER_STATE_DESC getDesc() const { return mData; }
  87. protected:
  88. friend class SamplerState;
  89. friend class SamplerStateCore;
  90. friend class SamplerStateRTTI;
  91. SAMPLER_STATE_DESC mData;
  92. UINT64 mHash;
  93. };
  94. /**
  95. * @brief Core thread version of a sampler state.
  96. *
  97. * @see SamplerState
  98. *
  99. * @note Core thread.
  100. */
  101. class BS_CORE_EXPORT SamplerStateCore : public CoreObjectCore
  102. {
  103. public:
  104. virtual ~SamplerStateCore();
  105. /**
  106. * @brief Returns information about the sampler state.
  107. */
  108. const SamplerProperties& getProperties() const;
  109. /**
  110. * @brief Returns the default sampler state.
  111. */
  112. static const SPtr<SamplerStateCore>& getDefault();
  113. protected:
  114. friend class RenderStateCoreManager;
  115. SamplerStateCore(const SAMPLER_STATE_DESC& desc);
  116. /**
  117. * @copydoc CoreObjectCore::initialize
  118. */
  119. void initialize() override;
  120. /**
  121. * @brief Creates any API-specific state objects.
  122. */
  123. virtual void createInternal() { }
  124. SamplerProperties mProperties;
  125. };
  126. /**
  127. * @brief Class representing the state of a texture sampler.
  128. *
  129. * @note Sampler units are used for retrieving and filtering data from
  130. * textures set in a GPU program. Sampler states are immutable.
  131. *
  132. * Sim thread.
  133. */
  134. class BS_CORE_EXPORT SamplerState : public IReflectable, public CoreObject
  135. {
  136. public:
  137. virtual ~SamplerState();
  138. /**
  139. * @brief Returns information about the sampler state.
  140. */
  141. const SamplerProperties& getProperties() const;
  142. /**
  143. * @brief Retrieves a core implementation of the sampler state usable only from the
  144. * core thread.
  145. */
  146. SPtr<SamplerStateCore> getCore() const;
  147. /**
  148. * @brief Creates a new sampler state using the provided descriptor structure.
  149. */
  150. static SamplerStatePtr create(const SAMPLER_STATE_DESC& desc);
  151. /**
  152. * @brief Returns the default sampler state.
  153. */
  154. static const SamplerStatePtr& getDefault();
  155. /**
  156. * @brief Generates a hash value from a sampler state descriptor.
  157. */
  158. static UINT64 generateHash(const SAMPLER_STATE_DESC& desc);
  159. protected:
  160. SamplerState(const SAMPLER_STATE_DESC& desc);
  161. /**
  162. * @copydoc CoreObjectCore::createCore
  163. */
  164. SPtr<CoreObjectCore> createCore() const override;
  165. SamplerProperties mProperties;
  166. friend class RenderStateManager;
  167. /************************************************************************/
  168. /* RTTI */
  169. /************************************************************************/
  170. public:
  171. friend class SamplerStateRTTI;
  172. static RTTITypeBase* getRTTIStatic();
  173. virtual RTTITypeBase* getRTTI() const override;
  174. };
  175. }
  176. /**
  177. * @brief Hash value generator for SAMPLER_STATE_DESC.
  178. */
  179. template<>
  180. struct std::hash<BansheeEngine::SAMPLER_STATE_DESC>
  181. {
  182. size_t operator()(const BansheeEngine::SAMPLER_STATE_DESC& value) const
  183. {
  184. return (size_t)BansheeEngine::SamplerState::generateHash(value);
  185. }
  186. };