BsRenderStateManager.h 11 KB

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