2
0

BsSamplerState.h 5.9 KB

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