BsRenderStateManager.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "BsModule.h"
  6. #include "BsBlendState.h"
  7. #include "BsRasterizerState.h"
  8. #include "BsDepthStencilState.h"
  9. #include "BsSamplerState.h"
  10. namespace BansheeEngine
  11. {
  12. /** @cond INTERNAL */
  13. /** @addtogroup RenderAPI
  14. * @{
  15. */
  16. /** Handles creation of various render states. */
  17. class BS_CORE_EXPORT RenderStateManager : public Module <RenderStateManager>
  18. {
  19. public:
  20. /** Creates and initializes a new SamplerState. */
  21. SamplerStatePtr createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  22. /** Creates and initializes a new DepthStencilState. */
  23. DepthStencilStatePtr createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  24. /** Creates and initializes a new RasterizerState. */
  25. RasterizerStatePtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  26. /** Creates and initializes a new BlendState. */
  27. BlendStatePtr createBlendState(const BLEND_STATE_DESC& desc) const;
  28. /**
  29. * Creates an uninitialized sampler state. Requires manual initialization after creation.
  30. *
  31. * @note Internal method.
  32. */
  33. SamplerStatePtr _createSamplerStatePtr(const SAMPLER_STATE_DESC& desc) const;
  34. /**
  35. * Creates an uninitialized depth-stencil state. Requires manual initialization after creation.
  36. *
  37. * @note Internal method.
  38. */
  39. DepthStencilStatePtr _createDepthStencilStatePtr(const DEPTH_STENCIL_STATE_DESC& desc) const;
  40. /**
  41. * Creates an uninitialized rasterizer state. Requires manual initialization after creation.
  42. *
  43. * @note Internal method.
  44. */
  45. RasterizerStatePtr _createRasterizerStatePtr(const RASTERIZER_STATE_DESC& desc) const;
  46. /**
  47. * Creates an uninitialized blend state. Requires manual initialization after creation.
  48. *
  49. * @note Internal method.
  50. */
  51. BlendStatePtr _createBlendStatePtr(const BLEND_STATE_DESC& desc) const;
  52. /** Gets a sampler state initialized with default options. */
  53. const SamplerStatePtr& getDefaultSamplerState() const;
  54. /** Gets a blend state initialized with default options. */
  55. const BlendStatePtr& getDefaultBlendState() const;
  56. /** Gets a rasterizer state initialized with default options. */
  57. const RasterizerStatePtr& getDefaultRasterizerState() const;
  58. /** Gets a depth stencil state initialized with default options. */
  59. const DepthStencilStatePtr& getDefaultDepthStencilState() const;
  60. private:
  61. friend class SamplerState;
  62. friend class BlendState;
  63. friend class RasterizerState;
  64. friend class DepthStencilState;
  65. mutable SamplerStatePtr mDefaultSamplerState;
  66. mutable BlendStatePtr mDefaultBlendState;
  67. mutable RasterizerStatePtr mDefaultRasterizerState;
  68. mutable DepthStencilStatePtr mDefaultDepthStencilState;
  69. };
  70. /** Handles creation of various render states. */
  71. class BS_CORE_EXPORT RenderStateCoreManager : public Module<RenderStateCoreManager>
  72. {
  73. private:
  74. /** Contains data about a cached blend state. */
  75. struct CachedBlendState
  76. {
  77. CachedBlendState()
  78. :id(0)
  79. { }
  80. CachedBlendState(UINT32 id)
  81. :id(id)
  82. { }
  83. std::weak_ptr<BlendStateCore> state;
  84. UINT32 id;
  85. };
  86. /** Contains data about a cached rasterizer state. */
  87. struct CachedRasterizerState
  88. {
  89. CachedRasterizerState()
  90. :id(0)
  91. { }
  92. CachedRasterizerState(UINT32 id)
  93. :id(id)
  94. { }
  95. std::weak_ptr<RasterizerStateCore> state;
  96. UINT32 id;
  97. };
  98. /** Contains data about a cached depth stencil state. */
  99. struct CachedDepthStencilState
  100. {
  101. CachedDepthStencilState()
  102. :id(0)
  103. { }
  104. CachedDepthStencilState(UINT32 id)
  105. :id(id)
  106. { }
  107. std::weak_ptr<DepthStencilStateCore> state;
  108. UINT32 id;
  109. };
  110. public:
  111. RenderStateCoreManager();
  112. /** @copydoc RenderStateManager::createSamplerState */
  113. SPtr<SamplerStateCore> createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  114. /** @copydoc RenderStateManager::createDepthStencilState */
  115. SPtr<DepthStencilStateCore> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  116. /** @copydoc RenderStateManager::createRasterizerState */
  117. SPtr<RasterizerStateCore> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  118. /** @copydoc RenderStateManager::createBlendState */
  119. SPtr<BlendStateCore> createBlendState(const BLEND_STATE_DESC& desc) const;
  120. /** Creates an uninitialized sampler state. Requires manual initialization after creation. */
  121. SPtr<SamplerStateCore> _createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  122. /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */
  123. SPtr<DepthStencilStateCore> _createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  124. /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */
  125. SPtr<RasterizerStateCore> _createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  126. /** Creates an uninitialized blend state. Requires manual initialization after creation. */
  127. SPtr<BlendStateCore> _createBlendState(const BLEND_STATE_DESC& desc) const;
  128. /** Gets a sampler state initialized with default options. */
  129. const SPtr<SamplerStateCore>& getDefaultSamplerState() const;
  130. /** Gets a blend state initialized with default options. */
  131. const SPtr<BlendStateCore>& getDefaultBlendState() const;
  132. /** Gets a rasterizer state initialized with default options. */
  133. const SPtr<RasterizerStateCore>& getDefaultRasterizerState() const;
  134. /** Gets a depth stencil state initialized with default options. */
  135. const SPtr<DepthStencilStateCore>& getDefaultDepthStencilState() const;
  136. protected:
  137. friend class SamplerState;
  138. friend class BlendState;
  139. friend class RasterizerState;
  140. friend class DepthStencilState;
  141. friend class SamplerStateCore;
  142. friend class BlendStateCore;
  143. friend class RasterizerStateCore;
  144. friend class DepthStencilStateCore;
  145. /** @copydoc Module::onShutDown */
  146. void onShutDown() override;
  147. /** @copydoc createSamplerState */
  148. virtual SPtr<SamplerStateCore> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc) const;
  149. /** @copydoc createBlendState */
  150. virtual SPtr<BlendStateCore> createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const;
  151. /** @copydoc createRasterizerState */
  152. virtual SPtr<RasterizerStateCore> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const;
  153. /** @copydoc createDepthStencilState */
  154. virtual SPtr<DepthStencilStateCore> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id) const;
  155. private:
  156. /** Triggered when a new sampler state is created. */
  157. void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateCore>& state) const;
  158. /** Triggered when a new sampler state is created. */
  159. void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const;
  160. /** Triggered when a new sampler state is created. */
  161. void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const;
  162. /** Triggered when a new sampler state is created. */
  163. void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const;
  164. /**
  165. * Triggered when the last reference to a specific sampler state is destroyed, which means we must clear our cached
  166. * version as well.
  167. */
  168. void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
  169. /**
  170. * Attempts to find a cached sampler state corresponding to the provided descriptor. Returns null if one doesn't
  171. * exist.
  172. */
  173. SPtr<SamplerStateCore> findCachedState(const SAMPLER_STATE_DESC& desc) const;
  174. /**
  175. * Attempts to find a cached blend state corresponding to the provided descriptor. Returns null if one doesn't exist.
  176. */
  177. SPtr<BlendStateCore> findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const;
  178. /**
  179. * Attempts to find a cached rasterizer state corresponding to the provided descriptor. Returns null if one doesn't
  180. * exist.
  181. */
  182. SPtr<RasterizerStateCore> findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const;
  183. /**
  184. * Attempts to find a cached depth-stencil state corresponding to the provided descriptor. Returns null if one
  185. * doesn't exist.
  186. */
  187. SPtr<DepthStencilStateCore> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const;
  188. mutable SPtr<SamplerStateCore> mDefaultSamplerState;
  189. mutable SPtr<BlendStateCore> mDefaultBlendState;
  190. mutable SPtr<RasterizerStateCore> mDefaultRasterizerState;
  191. mutable SPtr<DepthStencilStateCore> mDefaultDepthStencilState;
  192. mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerStateCore>> mCachedSamplerStates;
  193. mutable UnorderedMap<BLEND_STATE_DESC, CachedBlendState> mCachedBlendStates;
  194. mutable UnorderedMap<RASTERIZER_STATE_DESC, CachedRasterizerState> mCachedRasterizerStates;
  195. mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, CachedDepthStencilState> mCachedDepthStencilStates;
  196. mutable UINT32 mNextBlendStateId;
  197. mutable UINT32 mNextRasterizerStateId;
  198. mutable UINT32 mNextDepthStencilStateId;
  199. BS_MUTEX(mMutex);
  200. };
  201. /** @} */
  202. /** @endcond */
  203. }