BsSamplerState.h 6.0 KB

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