BsSamplerState.h 5.9 KB

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