BsRenderStateManager.h 12 KB

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