BsRenderStateManager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /**
  101. * @copydoc RenderStateManager::createSamplerState
  102. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  103. */
  104. SPtr<SamplerStateCore> createSamplerState(const SAMPLER_STATE_DESC& desc,
  105. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  106. /** @copydoc RenderStateManager::createDepthStencilState */
  107. SPtr<DepthStencilStateCore> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  108. /** @copydoc RenderStateManager::createRasterizerState */
  109. SPtr<RasterizerStateCore> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  110. /** @copydoc RenderStateManager::createBlendState */
  111. SPtr<BlendStateCore> createBlendState(const BLEND_STATE_DESC& desc) const;
  112. /**
  113. * @copydoc RenderStateManager::createPipelineState
  114. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  115. */
  116. SPtr<GpuPipelineStateCore> createPipelineState(const PIPELINE_STATE_CORE_DESC& desc,
  117. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  118. /** Creates an uninitialized sampler state. Requires manual initialization after creation. */
  119. SPtr<SamplerStateCore> _createSamplerState(const SAMPLER_STATE_DESC& desc,
  120. GpuDeviceFlags deviceMask = GDF_DEFAULT) 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. /** Creates an uninitialized GpuPipelineState. Requires manual initialization after creation. */
  128. virtual SPtr<GpuPipelineStateCore> _createPipelineState(const PIPELINE_STATE_CORE_DESC& desc,
  129. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  130. /** Gets a sampler state initialized with default options. */
  131. const SPtr<SamplerStateCore>& getDefaultSamplerState() const;
  132. /** Gets a blend state initialized with default options. */
  133. const SPtr<BlendStateCore>& getDefaultBlendState() const;
  134. /** Gets a rasterizer state initialized with default options. */
  135. const SPtr<RasterizerStateCore>& getDefaultRasterizerState() const;
  136. /** Gets a depth stencil state initialized with default options. */
  137. const SPtr<DepthStencilStateCore>& getDefaultDepthStencilState() const;
  138. protected:
  139. friend class SamplerState;
  140. friend class BlendState;
  141. friend class RasterizerState;
  142. friend class DepthStencilState;
  143. friend class SamplerStateCore;
  144. friend class BlendStateCore;
  145. friend class RasterizerStateCore;
  146. friend class DepthStencilStateCore;
  147. /** @copydoc Module::onShutDown */
  148. void onShutDown() override;
  149. /** @copydoc createSamplerState */
  150. virtual SPtr<SamplerStateCore> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask) const;
  151. /** @copydoc createBlendState */
  152. virtual SPtr<BlendStateCore> createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const;
  153. /** @copydoc createRasterizerState */
  154. virtual SPtr<RasterizerStateCore> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const;
  155. /** @copydoc createDepthStencilState */
  156. virtual SPtr<DepthStencilStateCore> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id) const;
  157. private:
  158. /** Triggered when a new sampler state is created. */
  159. void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateCore>& state) const;
  160. /** Triggered when a new sampler state is created. */
  161. void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const;
  162. /** Triggered when a new sampler state is created. */
  163. void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const;
  164. /** Triggered when a new sampler state is created. */
  165. void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const;
  166. /**
  167. * Triggered when the last reference to a specific sampler state is destroyed, which means we must clear our cached
  168. * version as well.
  169. */
  170. void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
  171. /**
  172. * Attempts to find a cached sampler state corresponding to the provided descriptor. Returns null if one doesn't
  173. * exist.
  174. */
  175. SPtr<SamplerStateCore> findCachedState(const SAMPLER_STATE_DESC& desc) const;
  176. /**
  177. * Attempts to find a cached blend state corresponding to the provided descriptor. Returns null if one doesn't exist.
  178. */
  179. SPtr<BlendStateCore> findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const;
  180. /**
  181. * Attempts to find a cached rasterizer state corresponding to the provided descriptor. Returns null if one doesn't
  182. * exist.
  183. */
  184. SPtr<RasterizerStateCore> findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const;
  185. /**
  186. * Attempts to find a cached depth-stencil state corresponding to the provided descriptor. Returns null if one
  187. * doesn't exist.
  188. */
  189. SPtr<DepthStencilStateCore> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const;
  190. mutable SPtr<SamplerStateCore> mDefaultSamplerState;
  191. mutable SPtr<BlendStateCore> mDefaultBlendState;
  192. mutable SPtr<RasterizerStateCore> mDefaultRasterizerState;
  193. mutable SPtr<DepthStencilStateCore> mDefaultDepthStencilState;
  194. mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerStateCore>> mCachedSamplerStates;
  195. mutable UnorderedMap<BLEND_STATE_DESC, CachedBlendState> mCachedBlendStates;
  196. mutable UnorderedMap<RASTERIZER_STATE_DESC, CachedRasterizerState> mCachedRasterizerStates;
  197. mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, CachedDepthStencilState> mCachedDepthStencilStates;
  198. mutable UINT32 mNextBlendStateId;
  199. mutable UINT32 mNextRasterizerStateId;
  200. mutable UINT32 mNextDepthStencilStateId;
  201. mutable Mutex mMutex;
  202. };
  203. /** @} */
  204. }