BsSamplerState.h 6.9 KB

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