BsRenderStateManager.h 9.2 KB

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