BsRenderStateManager.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 Common methods and data for both sim and core thread versions
  12. * of RenderStateManager.
  13. */
  14. template<bool Core>
  15. class BS_CORE_EXPORT TRenderStateManager
  16. {
  17. public:
  18. virtual ~TRenderStateManager() { }
  19. protected:
  20. template<bool Core> struct TSamplerStateType { };
  21. template<> struct TSamplerStateType < false > { typedef SamplerState Type; };
  22. template<> struct TSamplerStateType < true > { typedef SamplerStateCore Type; };
  23. template<bool Core> struct TBlendStateType { };
  24. template<> struct TBlendStateType < false > { typedef BlendState Type; };
  25. template<> struct TBlendStateType < true > { typedef BlendStateCore Type; };
  26. template<bool Core> struct TRasterizerStateType { };
  27. template<> struct TRasterizerStateType < false > { typedef RasterizerState Type; };
  28. template<> struct TRasterizerStateType < true > { typedef RasterizerStateCore Type; };
  29. template<bool Core> struct TDepthStencilStateType { };
  30. template<> struct TDepthStencilStateType < false > { typedef DepthStencilState Type; };
  31. template<> struct TDepthStencilStateType < true > { typedef DepthStencilStateCore Type; };
  32. typedef typename TSamplerStateType<Core>::Type SamplerStateType;
  33. typedef typename TBlendStateType<Core>::Type BlendStateType;
  34. typedef typename TRasterizerStateType<Core>::Type RasterizerStateType;
  35. typedef typename TDepthStencilStateType<Core>::Type DepthStencilStateType;
  36. /**
  37. * @brief Triggered when a new sampler state is created.
  38. */
  39. void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateType>& state) const;
  40. /**
  41. * @brief Triggered when a new sampler state is created.
  42. */
  43. void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const SPtr<BlendStateType>& state) const;
  44. /**
  45. * @brief Triggered when a new sampler state is created.
  46. */
  47. void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const SPtr<RasterizerStateType>& state) const;
  48. /**
  49. * @brief Triggered when a new sampler state is created.
  50. */
  51. void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const SPtr<DepthStencilStateType>& state) const;
  52. /**
  53. * @brief Triggered when the last reference to a specific sampler state is destroyed, which
  54. * means we must clear our cached version as well.
  55. */
  56. void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
  57. /**
  58. * @brief Triggered when the last reference to a specific blend state is destroyed, which
  59. * means we must clear our cached version as well.
  60. */
  61. void notifyBlendStateDestroyed(const BLEND_STATE_DESC& desc) const;
  62. /**
  63. * @brief Triggered when the last reference to a specific rasterizer state is destroyed, which
  64. * means we must clear our cached version as well.
  65. */
  66. void notifyRasterizerStateDestroyed(const RASTERIZER_STATE_DESC& desc) const;
  67. /**
  68. * @brief Triggered when the last reference to a specific depth stencil state is destroyed, which
  69. * means we must clear our cached version as well.
  70. */
  71. void notifyDepthStencilStateDestroyed(const DEPTH_STENCIL_STATE_DESC& desc) const;
  72. /**
  73. * @brief Attempts to find a cached sampler state corresponding to the provided descriptor.
  74. * Returns null if one doesn't exist.
  75. */
  76. SPtr<SamplerStateType> findCachedState(const SAMPLER_STATE_DESC& desc) const;
  77. /**
  78. * @brief Attempts to find a cached blend state corresponding to the provided descriptor.
  79. * Returns null if one doesn't exist.
  80. */
  81. SPtr<BlendStateType> findCachedState(const BLEND_STATE_DESC& desc) const;
  82. /**
  83. * @brief Attempts to find a cached rasterizer state corresponding to the provided descriptor.
  84. * Returns null if one doesn't exist.
  85. */
  86. SPtr<RasterizerStateType> findCachedState(const RASTERIZER_STATE_DESC& desc) const;
  87. /**
  88. * @brief Attempts to find a cached depth-stencil state corresponding to the provided descriptor.
  89. * Returns null if one doesn't exist.
  90. */
  91. SPtr<DepthStencilStateType> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  92. mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerStateType>> mCachedSamplerStates;
  93. mutable UnorderedMap<BLEND_STATE_DESC, std::weak_ptr<BlendStateType>> mCachedBlendStates;
  94. mutable UnorderedMap<RASTERIZER_STATE_DESC, std::weak_ptr<RasterizerStateType>> mCachedRasterizerStates;
  95. mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, std::weak_ptr<DepthStencilStateType>> mCachedDepthStencilStates;
  96. BS_MUTEX(mMutex);
  97. };
  98. /**
  99. * @brief Handles creation of various render states.
  100. */
  101. class BS_CORE_EXPORT RenderStateManager : public Module <RenderStateManager>, TRenderStateManager<false>
  102. {
  103. public:
  104. /**
  105. * @brief Creates and initializes a new SamplerState.
  106. */
  107. SamplerStatePtr createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  108. /**
  109. * @brief Creates and initializes a new DepthStencilState.
  110. */
  111. DepthStencilStatePtr createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  112. /**
  113. * @brief Creates and initializes a new RasterizerState.
  114. */
  115. RasterizerStatePtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  116. /**
  117. * @brief Creates and initializes a new BlendState.
  118. */
  119. BlendStatePtr createBlendState(const BLEND_STATE_DESC& desc) const;
  120. /**
  121. * @brief Creates an uninitialized sampler state. Requires manual initialization
  122. * after creation.
  123. *
  124. * @note Internal method.
  125. */
  126. SamplerStatePtr _createSamplerStatePtr(const SAMPLER_STATE_DESC& desc) const;
  127. /**
  128. * @brief Creates an uninitialized depth-stencil state. Requires manual initialization
  129. * after creation.
  130. *
  131. * @note Internal method.
  132. */
  133. DepthStencilStatePtr _createDepthStencilStatePtr(const DEPTH_STENCIL_STATE_DESC& desc) const;
  134. /**
  135. * @brief Creates an uninitialized rasterizer state. Requires manual initialization
  136. * after creation.
  137. *
  138. * @note Internal method.
  139. */
  140. RasterizerStatePtr _createRasterizerStatePtr(const RASTERIZER_STATE_DESC& desc) const;
  141. /**
  142. * @brief Creates an uninitialized blend state. Requires manual initialization
  143. * after creation.
  144. *
  145. * @note Internal method.
  146. */
  147. BlendStatePtr _createBlendStatePtr(const BLEND_STATE_DESC& desc) const;
  148. /**
  149. * @brief Gets a sampler state initialized with default options.
  150. */
  151. const SamplerStatePtr& getDefaultSamplerState() const;
  152. /**
  153. * @brief Gets a blend state initialized with default options.
  154. */
  155. const BlendStatePtr& getDefaultBlendState() const;
  156. /**
  157. * @brief Gets a rasterizer state initialized with default options.
  158. */
  159. const RasterizerStatePtr& getDefaultRasterizerState() const;
  160. /**
  161. * @brief Gets a depth stencil state initialized with default options.
  162. */
  163. const DepthStencilStatePtr& getDefaultDepthStencilState() const;
  164. private:
  165. friend class SamplerState;
  166. friend class BlendState;
  167. friend class RasterizerState;
  168. friend class DepthStencilState;
  169. mutable SamplerStatePtr mDefaultSamplerState;
  170. mutable BlendStatePtr mDefaultBlendState;
  171. mutable RasterizerStatePtr mDefaultRasterizerState;
  172. mutable DepthStencilStatePtr mDefaultDepthStencilState;
  173. };
  174. /**
  175. * @brief Handles creation of various render states.
  176. */
  177. class BS_CORE_EXPORT RenderStateCoreManager : public Module<RenderStateCoreManager>, TRenderStateManager<true>
  178. {
  179. public:
  180. /**
  181. * @copydoc RenderStateManager::createSamplerState
  182. */
  183. SPtr<SamplerStateCore> createSamplerState(const SAMPLER_STATE_DESC& desc) const;
  184. /**
  185. * @copydoc RenderStateManager::createDepthStencilState
  186. */
  187. SPtr<DepthStencilStateCore> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
  188. /**
  189. * @copydoc RenderStateManager::createRasterizerState
  190. */
  191. SPtr<RasterizerStateCore> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
  192. /**
  193. * @copydoc RenderStateManager::createBlendState
  194. */
  195. SPtr<BlendStateCore> createBlendState(const BLEND_STATE_DESC& desc) const;
  196. /**
  197. * @brief Gets a sampler state initialized with default options.
  198. */
  199. const SPtr<SamplerStateCore>& getDefaultSamplerState() const;
  200. /**
  201. * @brief Gets a blend state initialized with default options.
  202. */
  203. const SPtr<BlendStateCore>& getDefaultBlendState() const;
  204. /**
  205. * @brief Gets a rasterizer state initialized with default options.
  206. */
  207. const SPtr<RasterizerStateCore>& getDefaultRasterizerState() const;
  208. /**
  209. * @brief Gets a depth stencil state initialized with default options.
  210. */
  211. const SPtr<DepthStencilStateCore>& getDefaultDepthStencilState() const;
  212. private:
  213. mutable SPtr<SamplerStateCore> mDefaultSamplerState;
  214. mutable SPtr<BlendStateCore> mDefaultBlendState;
  215. mutable SPtr<RasterizerStateCore> mDefaultRasterizerState;
  216. mutable SPtr<DepthStencilStateCore> mDefaultDepthStencilState;
  217. protected:
  218. friend class SamplerState;
  219. friend class BlendState;
  220. friend class RasterizerState;
  221. friend class DepthStencilState;
  222. friend class SamplerStateCore;
  223. friend class BlendStateCore;
  224. friend class RasterizerStateCore;
  225. friend class DepthStencilStateCore;
  226. /**
  227. * @copydoc createSamplerState
  228. */
  229. virtual SPtr<SamplerStateCore> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc) const;
  230. /**
  231. * @copydoc createBlendState
  232. */
  233. virtual SPtr<BlendStateCore> createBlendStateInternal(const BLEND_STATE_DESC& desc) const;
  234. /**
  235. * @copydoc createRasterizerState
  236. */
  237. virtual SPtr<RasterizerStateCore> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc) const;
  238. /**
  239. * @copydoc createDepthStencilState
  240. */
  241. virtual SPtr<DepthStencilStateCore> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc) const;
  242. };
  243. }