BsRenderStateManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /** Handles creation of various render states. */
  76. class BS_CORE_EXPORT RenderStateCoreManager : public Module<RenderStateCoreManager>
  77. {
  78. private:
  79. /** Contains data about a cached blend state. */
  80. struct CachedBlendState
  81. {
  82. CachedBlendState()
  83. :id(0)
  84. { }
  85. CachedBlendState(UINT32 id)
  86. :id(id)
  87. { }
  88. std::weak_ptr<BlendStateCore> state;
  89. UINT32 id;
  90. };
  91. /** Contains data about a cached rasterizer state. */
  92. struct CachedRasterizerState
  93. {
  94. CachedRasterizerState()
  95. :id(0)
  96. { }
  97. CachedRasterizerState(UINT32 id)
  98. :id(id)
  99. { }
  100. std::weak_ptr<RasterizerStateCore> state;
  101. UINT32 id;
  102. };
  103. /** Contains data about a cached depth stencil state. */
  104. struct CachedDepthStencilState
  105. {
  106. CachedDepthStencilState()
  107. :id(0)
  108. { }
  109. CachedDepthStencilState(UINT32 id)
  110. :id(id)
  111. { }
  112. std::weak_ptr<DepthStencilStateCore> state;
  113. UINT32 id;
  114. };
  115. public:
  116. RenderStateCoreManager();
  117. /**
  118. * @copydoc RenderStateManager::createSamplerState
  119. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  120. */
  121. SPtr<SamplerStateCore> createSamplerState(const SAMPLER_STATE_DESC& desc,
  122. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  123. /** @copydoc RenderStateManager::createDepthStencilState */
  124. SPtr<DepthStencilStateCore> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  125. /** @copydoc RenderStateManager::createRasterizerState */
  126. SPtr<RasterizerStateCore> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  127. /** @copydoc RenderStateManager::createBlendState */
  128. SPtr<BlendStateCore> createBlendState(const BLEND_STATE_DESC& desc) const;
  129. /**
  130. * @copydoc RenderStateManager::createGraphicsPipelineState
  131. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  132. */
  133. SPtr<GraphicsPipelineStateCore> createGraphicsPipelineState(const PIPELINE_STATE_CORE_DESC& desc,
  134. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  135. /**
  136. * @copydoc RenderStateManager::createComputePipelineState
  137. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  138. */
  139. SPtr<ComputePipelineStateCore> createComputePipelineState(const SPtr<GpuProgramCore>& program,
  140. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  141. /** @copydoc GpuPipelineParamInfoCore::create */
  142. SPtr<GpuPipelineParamInfoCore> createPipelineParamInfo(const GPU_PIPELINE_PARAMS_DESC& desc,
  143. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  144. /** Creates an uninitialized sampler state. Requires manual initialization after creation. */
  145. SPtr<SamplerStateCore> _createSamplerState(const SAMPLER_STATE_DESC& desc,
  146. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  147. /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */
  148. SPtr<DepthStencilStateCore> _createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  149. /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */
  150. SPtr<RasterizerStateCore> _createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  151. /** Creates an uninitialized blend state. Requires manual initialization after creation. */
  152. SPtr<BlendStateCore> _createBlendState(const BLEND_STATE_DESC& desc) const;
  153. /** Creates an uninitialized GraphicsPipelineState. Requires manual initialization after creation. */
  154. virtual SPtr<GraphicsPipelineStateCore> _createGraphicsPipelineState(const PIPELINE_STATE_CORE_DESC& desc,
  155. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  156. /** Creates an uninitialized ComputePipelineState. Requires manual initialization after creation. */
  157. virtual SPtr<ComputePipelineStateCore> _createComputePipelineState(const SPtr<GpuProgramCore>& program,
  158. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  159. /** Creates an uninitialized GpuPipelineParamInfoCore. Requires manual initialization after creation. */
  160. virtual SPtr<GpuPipelineParamInfoCore> _createPipelineParamInfo(const GPU_PIPELINE_PARAMS_DESC& desc,
  161. GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
  162. /** Gets a sampler state initialized with default options. */
  163. const SPtr<SamplerStateCore>& getDefaultSamplerState() const;
  164. /** Gets a blend state initialized with default options. */
  165. const SPtr<BlendStateCore>& getDefaultBlendState() const;
  166. /** Gets a rasterizer state initialized with default options. */
  167. const SPtr<RasterizerStateCore>& getDefaultRasterizerState() const;
  168. /** Gets a depth stencil state initialized with default options. */
  169. const SPtr<DepthStencilStateCore>& getDefaultDepthStencilState() const;
  170. protected:
  171. friend class SamplerState;
  172. friend class BlendState;
  173. friend class RasterizerState;
  174. friend class DepthStencilState;
  175. friend class SamplerStateCore;
  176. friend class BlendStateCore;
  177. friend class RasterizerStateCore;
  178. friend class DepthStencilStateCore;
  179. /** @copydoc Module::onShutDown */
  180. void onShutDown() override;
  181. /** @copydoc createSamplerState */
  182. virtual SPtr<SamplerStateCore> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask) const;
  183. /** @copydoc createBlendState */
  184. virtual SPtr<BlendStateCore> createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const;
  185. /** @copydoc createRasterizerState */
  186. virtual SPtr<RasterizerStateCore> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const;
  187. /** @copydoc createDepthStencilState */
  188. virtual SPtr<DepthStencilStateCore> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id) const;
  189. private:
  190. /** Triggered when a new sampler state is created. */
  191. void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateCore>& state) const;
  192. /** Triggered when a new sampler state is created. */
  193. void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const;
  194. /** Triggered when a new sampler state is created. */
  195. void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const;
  196. /** Triggered when a new sampler state is created. */
  197. void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const;
  198. /**
  199. * Triggered when the last reference to a specific sampler state is destroyed, which means we must clear our cached
  200. * version as well.
  201. */
  202. void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
  203. /**
  204. * Attempts to find a cached sampler state corresponding to the provided descriptor. Returns null if one doesn't
  205. * exist.
  206. */
  207. SPtr<SamplerStateCore> findCachedState(const SAMPLER_STATE_DESC& desc) const;
  208. /**
  209. * Attempts to find a cached blend state corresponding to the provided descriptor. Returns null if one doesn't exist.
  210. */
  211. SPtr<BlendStateCore> findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const;
  212. /**
  213. * Attempts to find a cached rasterizer state corresponding to the provided descriptor. Returns null if one doesn't
  214. * exist.
  215. */
  216. SPtr<RasterizerStateCore> findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const;
  217. /**
  218. * Attempts to find a cached depth-stencil state corresponding to the provided descriptor. Returns null if one
  219. * doesn't exist.
  220. */
  221. SPtr<DepthStencilStateCore> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const;
  222. mutable SPtr<SamplerStateCore> mDefaultSamplerState;
  223. mutable SPtr<BlendStateCore> mDefaultBlendState;
  224. mutable SPtr<RasterizerStateCore> mDefaultRasterizerState;
  225. mutable SPtr<DepthStencilStateCore> mDefaultDepthStencilState;
  226. mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerStateCore>> mCachedSamplerStates;
  227. mutable UnorderedMap<BLEND_STATE_DESC, CachedBlendState> mCachedBlendStates;
  228. mutable UnorderedMap<RASTERIZER_STATE_DESC, CachedRasterizerState> mCachedRasterizerStates;
  229. mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, CachedDepthStencilState> mCachedDepthStencilStates;
  230. mutable UINT32 mNextBlendStateId;
  231. mutable UINT32 mNextRasterizerStateId;
  232. mutable UINT32 mNextDepthStencilStateId;
  233. mutable Mutex mMutex;
  234. };
  235. /** @} */
  236. }