BsRenderStateManager.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsBlendState.h"
  5. #include "BsRasterizerState.h"
  6. #include "BsDepthStencilState.h"
  7. #include "BsSamplerState.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Handles creation of various render states.
  12. */
  13. class BS_CORE_EXPORT RenderStateManager : public Module <RenderStateManager>
  14. {
  15. public:
  16. /**
  17. * @brief Creates and initializes a new SamplerState.
  18. */
  19. SamplerStatePtr createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  20. /**
  21. * @brief Creates and initializes a new DepthStencilState.
  22. */
  23. DepthStencilStatePtr createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  24. /**
  25. * @brief Creates and initializes a new RasterizerState.
  26. */
  27. RasterizerStatePtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  28. /**
  29. * @brief Creates and initializes a new BlendState.
  30. */
  31. BlendStatePtr createBlendState(const BLEND_STATE_DESC& desc) const;
  32. /**
  33. * @brief Creates an uninitialized sampler state. Requires manual initialization
  34. * after creation.
  35. *
  36. * @note Internal method.
  37. */
  38. SamplerStatePtr _createSamplerStatePtr(const SAMPLER_STATE_DESC& desc) const;
  39. /**
  40. * @brief Creates an uninitialized depth-stencil state. Requires manual initialization
  41. * after creation.
  42. *
  43. * @note Internal method.
  44. */
  45. DepthStencilStatePtr _createDepthStencilStatePtr(const DEPTH_STENCIL_STATE_DESC& desc) const;
  46. /**
  47. * @brief Creates an uninitialized rasterizer state. Requires manual initialization
  48. * after creation.
  49. *
  50. * @note Internal method.
  51. */
  52. RasterizerStatePtr _createRasterizerStatePtr(const RASTERIZER_STATE_DESC& desc) const;
  53. /**
  54. * @brief Creates an uninitialized blend state. Requires manual initialization
  55. * after creation.
  56. *
  57. * @note Internal method.
  58. */
  59. BlendStatePtr _createBlendStatePtr(const BLEND_STATE_DESC& desc) const;
  60. /**
  61. * @brief Gets a sampler state initialized with default options.
  62. */
  63. const SamplerStatePtr& getDefaultSamplerState() const;
  64. /**
  65. * @brief Gets a blend state initialized with default options.
  66. */
  67. const BlendStatePtr& getDefaultBlendState() const;
  68. /**
  69. * @brief Gets a rasterizer state initialized with default options.
  70. */
  71. const RasterizerStatePtr& getDefaultRasterizerState() const;
  72. /**
  73. * @brief Gets a depth stencil state initialized with default options.
  74. */
  75. const DepthStencilStatePtr& getDefaultDepthStencilState() const;
  76. private:
  77. friend class SamplerState;
  78. friend class BlendState;
  79. friend class RasterizerState;
  80. friend class DepthStencilState;
  81. mutable SamplerStatePtr mDefaultSamplerState;
  82. mutable BlendStatePtr mDefaultBlendState;
  83. mutable RasterizerStatePtr mDefaultRasterizerState;
  84. mutable DepthStencilStatePtr mDefaultDepthStencilState;
  85. };
  86. /**
  87. * @brief Handles creation of various render states.
  88. */
  89. class BS_CORE_EXPORT RenderStateCoreManager : public Module<RenderStateCoreManager>
  90. {
  91. private:
  92. /**
  93. * @brief Contains data about a cached blend state
  94. */
  95. struct CachedBlendState
  96. {
  97. CachedBlendState()
  98. :id(0)
  99. { }
  100. CachedBlendState(UINT32 id)
  101. :id(id)
  102. { }
  103. std::weak_ptr<BlendStateCore> state;
  104. UINT32 id;
  105. };
  106. /**
  107. * @brief Contains data about a cached blend state
  108. */
  109. struct CachedRasterizerState
  110. {
  111. CachedRasterizerState()
  112. :id(0)
  113. { }
  114. CachedRasterizerState(UINT32 id)
  115. :id(id)
  116. { }
  117. std::weak_ptr<RasterizerStateCore> state;
  118. UINT32 id;
  119. };
  120. /**
  121. * @brief Contains data about a cached blend state
  122. */
  123. struct CachedDepthStencilState
  124. {
  125. CachedDepthStencilState()
  126. :id(0)
  127. { }
  128. CachedDepthStencilState(UINT32 id)
  129. :id(id)
  130. { }
  131. std::weak_ptr<DepthStencilStateCore> state;
  132. UINT32 id;
  133. };
  134. public:
  135. RenderStateCoreManager();
  136. /**
  137. * @copydoc RenderStateManager::createSamplerState
  138. */
  139. SPtr<SamplerStateCore> createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  140. /**
  141. * @copydoc RenderStateManager::createDepthStencilState
  142. */
  143. SPtr<DepthStencilStateCore> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  144. /**
  145. * @copydoc RenderStateManager::createRasterizerState
  146. */
  147. SPtr<RasterizerStateCore> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  148. /**
  149. * @copydoc RenderStateManager::createBlendState
  150. */
  151. SPtr<BlendStateCore> createBlendState(const BLEND_STATE_DESC& desc) const;
  152. /**
  153. * @brief Creates an uninitialized sampler state. Requires manual initialization
  154. * after creation.
  155. *
  156. * @note Internal method.
  157. */
  158. SPtr<SamplerStateCore> _createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  159. /**
  160. * @brief Creates an uninitialized depth-stencil state. Requires manual initialization
  161. * after creation.
  162. *
  163. * @note Internal method.
  164. */
  165. SPtr<DepthStencilStateCore> _createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  166. /**
  167. * @brief Creates an uninitialized rasterizer state. Requires manual initialization
  168. * after creation.
  169. *
  170. * @note Internal method.
  171. */
  172. SPtr<RasterizerStateCore> _createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  173. /**
  174. * @brief Creates an uninitialized blend state. Requires manual initialization
  175. * after creation.
  176. *
  177. * @note Internal method.
  178. */
  179. SPtr<BlendStateCore> _createBlendState(const BLEND_STATE_DESC& desc) const;
  180. /**
  181. * @brief Gets a sampler state initialized with default options.
  182. */
  183. const SPtr<SamplerStateCore>& getDefaultSamplerState() const;
  184. /**
  185. * @brief Gets a blend state initialized with default options.
  186. */
  187. const SPtr<BlendStateCore>& getDefaultBlendState() const;
  188. /**
  189. * @brief Gets a rasterizer state initialized with default options.
  190. */
  191. const SPtr<RasterizerStateCore>& getDefaultRasterizerState() const;
  192. /**
  193. * @brief Gets a depth stencil state initialized with default options.
  194. */
  195. const SPtr<DepthStencilStateCore>& getDefaultDepthStencilState() const;
  196. protected:
  197. friend class SamplerState;
  198. friend class BlendState;
  199. friend class RasterizerState;
  200. friend class DepthStencilState;
  201. friend class SamplerStateCore;
  202. friend class BlendStateCore;
  203. friend class RasterizerStateCore;
  204. friend class DepthStencilStateCore;
  205. /**
  206. * @copydoc createSamplerState
  207. */
  208. virtual SPtr<SamplerStateCore> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc) const;
  209. /**
  210. * @copydoc createBlendState
  211. */
  212. virtual SPtr<BlendStateCore> createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const;
  213. /**
  214. * @copydoc createRasterizerState
  215. */
  216. virtual SPtr<RasterizerStateCore> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const;
  217. /**
  218. * @copydoc createDepthStencilState
  219. */
  220. virtual SPtr<DepthStencilStateCore> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id) const;
  221. private:
  222. /**
  223. * @brief Triggered when a new sampler state is created.
  224. */
  225. void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateCore>& state) const;
  226. /**
  227. * @brief Triggered when a new sampler state is created.
  228. */
  229. void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const;
  230. /**
  231. * @brief Triggered when a new sampler state is created.
  232. */
  233. void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const;
  234. /**
  235. * @brief Triggered when a new sampler state is created.
  236. */
  237. void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const;
  238. /**
  239. * @brief Triggered when the last reference to a specific sampler state is destroyed, which
  240. * means we must clear our cached version as well.
  241. */
  242. void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
  243. /**
  244. * @brief Attempts to find a cached sampler state corresponding to the provided descriptor.
  245. * Returns null if one doesn't exist.
  246. */
  247. SPtr<SamplerStateCore> findCachedState(const SAMPLER_STATE_DESC& desc) const;
  248. /**
  249. * @brief Attempts to find a cached blend state corresponding to the provided descriptor.
  250. * Returns null if one doesn't exist.
  251. */
  252. SPtr<BlendStateCore> findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const;
  253. /**
  254. * @brief Attempts to find a cached rasterizer state corresponding to the provided descriptor.
  255. * Returns null if one doesn't exist.
  256. */
  257. SPtr<RasterizerStateCore> findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const;
  258. /**
  259. * @brief Attempts to find a cached depth-stencil state corresponding to the provided descriptor.
  260. * Returns null if one doesn't exist.
  261. */
  262. SPtr<DepthStencilStateCore> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const;
  263. mutable SPtr<SamplerStateCore> mDefaultSamplerState;
  264. mutable SPtr<BlendStateCore> mDefaultBlendState;
  265. mutable SPtr<RasterizerStateCore> mDefaultRasterizerState;
  266. mutable SPtr<DepthStencilStateCore> mDefaultDepthStencilState;
  267. mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerStateCore>> mCachedSamplerStates;
  268. mutable UnorderedMap<BLEND_STATE_DESC, CachedBlendState> mCachedBlendStates;
  269. mutable UnorderedMap<RASTERIZER_STATE_DESC, CachedRasterizerState> mCachedRasterizerStates;
  270. mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, CachedDepthStencilState> mCachedDepthStencilStates;
  271. mutable UINT32 mNextBlendStateId;
  272. mutable UINT32 mNextRasterizerStateId;
  273. mutable UINT32 mNextDepthStencilStateId;
  274. BS_MUTEX(mMutex);
  275. };
  276. }