BsRenderStateManager.h 9.1 KB

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