BsRenderStateManager.h 9.4 KB

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