BsRenderStateManager.h 12 KB

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