BsSamplerState.h 6.2 KB

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