BsRenderStateManager.h 9.9 KB

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