BsGpuPipelineState.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "CoreThread/BsCoreObject.h"
  6. namespace bs
  7. {
  8. /** @addtogroup RenderAPI
  9. * @{
  10. */
  11. /** Descriptor structure used for initializing a GPU pipeline state. */
  12. struct PIPELINE_STATE_DESC
  13. {
  14. SPtr<BlendState> blendState;
  15. SPtr<RasterizerState> rasterizerState;
  16. SPtr<DepthStencilState> depthStencilState;
  17. SPtr<GpuProgram> vertexProgram;
  18. SPtr<GpuProgram> fragmentProgram;
  19. SPtr<GpuProgram> geometryProgram;
  20. SPtr<GpuProgram> hullProgram;
  21. SPtr<GpuProgram> domainProgram;
  22. };
  23. /** @} */
  24. namespace ct
  25. {
  26. /** @addtogroup RenderAPI-Internal
  27. * @{
  28. */
  29. /** Descriptor structure used for initializing a GPU pipeline state. */
  30. struct PIPELINE_STATE_DESC
  31. {
  32. SPtr<BlendState> blendState;
  33. SPtr<RasterizerState> rasterizerState;
  34. SPtr<DepthStencilState> depthStencilState;
  35. SPtr<GpuProgram> vertexProgram;
  36. SPtr<GpuProgram> fragmentProgram;
  37. SPtr<GpuProgram> geometryProgram;
  38. SPtr<GpuProgram> hullProgram;
  39. SPtr<GpuProgram> domainProgram;
  40. };
  41. /** @} */
  42. }
  43. /** @addtogroup Implementation
  44. * @{
  45. */
  46. /** Contains all data used by a GPU pipeline state, templated so it may contain both core and sim thread data. */
  47. template<bool Core>
  48. struct TGpuPipelineStateTypes
  49. { };
  50. template<>
  51. struct TGpuPipelineStateTypes < false >
  52. {
  53. typedef SPtr<BlendState> BlendStateType;
  54. typedef SPtr<RasterizerState> RasterizerStateType;
  55. typedef SPtr<DepthStencilState> DepthStencilStateType;
  56. typedef SPtr<GpuProgram> GpuProgramType;
  57. typedef GpuPipelineParamInfo GpuPipelineParamInfoType;
  58. typedef PIPELINE_STATE_DESC StateDescType;
  59. };
  60. template<>
  61. struct TGpuPipelineStateTypes < true >
  62. {
  63. typedef SPtr<ct::BlendState> BlendStateType;
  64. typedef SPtr<ct::RasterizerState> RasterizerStateType;
  65. typedef SPtr<ct::DepthStencilState> DepthStencilStateType;
  66. typedef SPtr<ct::GpuProgram> GpuProgramType;
  67. typedef ct::GpuPipelineParamInfo GpuPipelineParamInfoType;
  68. typedef ct::PIPELINE_STATE_DESC StateDescType;
  69. };
  70. /**
  71. * Templated version of GraphicsPipelineState so it can be used for both core and non-core versions of the pipeline
  72. * state.
  73. */
  74. template<bool Core>
  75. class BS_CORE_EXPORT TGraphicsPipelineState
  76. {
  77. public:
  78. typedef typename TGpuPipelineStateTypes<Core>::BlendStateType BlendStateType;
  79. typedef typename TGpuPipelineStateTypes<Core>::RasterizerStateType RasterizerStateType;
  80. typedef typename TGpuPipelineStateTypes<Core>::DepthStencilStateType DepthStencilStateType;
  81. typedef typename TGpuPipelineStateTypes<Core>::GpuProgramType GpuProgramType;
  82. typedef typename TGpuPipelineStateTypes<Core>::StateDescType StateDescType;
  83. typedef typename TGpuPipelineStateTypes<Core>::GpuPipelineParamInfoType GpuPipelineParamInfoType;
  84. virtual ~TGraphicsPipelineState() { }
  85. bool hasVertexProgram() const { return mData.vertexProgram != nullptr; }
  86. bool hasFragmentProgram() const { return mData.fragmentProgram != nullptr; }
  87. bool hasGeometryProgram() const { return mData.geometryProgram != nullptr; }
  88. bool hasHullProgram() const { return mData.hullProgram != nullptr; }
  89. bool hasDomainProgram() const { return mData.domainProgram != nullptr; }
  90. BlendStateType getBlendState() const { return mData.blendState; }
  91. RasterizerStateType getRasterizerState() const { return mData.rasterizerState; }
  92. DepthStencilStateType getDepthStencilState() const { return mData.depthStencilState; }
  93. const GpuProgramType& getVertexProgram() const { return mData.vertexProgram; }
  94. const GpuProgramType& getFragmentProgram() const { return mData.fragmentProgram; }
  95. const GpuProgramType& getGeometryProgram() const { return mData.geometryProgram; }
  96. const GpuProgramType& getHullProgram() const { return mData.hullProgram; }
  97. const GpuProgramType& getDomainProgram() const { return mData.domainProgram; }
  98. /** Returns an object containing meta-data for parameters of all GPU programs used in this pipeline state. */
  99. const SPtr<GpuPipelineParamInfoType>& getParamInfo() const { return mParamInfo; }
  100. protected:
  101. TGraphicsPipelineState();
  102. TGraphicsPipelineState(const StateDescType& desc);
  103. StateDescType mData;
  104. SPtr<GpuPipelineParamInfoType> mParamInfo;
  105. };
  106. /**
  107. * Templated version of ComputePipelineState so it can be used for both core and non-core versions of the pipeline
  108. * state.
  109. */
  110. template<bool Core>
  111. class BS_CORE_EXPORT TComputePipelineState
  112. {
  113. public:
  114. typedef typename TGpuPipelineStateTypes<Core>::GpuProgramType GpuProgramType;
  115. typedef typename TGpuPipelineStateTypes<Core>::GpuPipelineParamInfoType GpuPipelineParamInfoType;
  116. virtual ~TComputePipelineState() { }
  117. const GpuProgramType& getProgram() const { return mProgram; }
  118. /** Returns an object containing meta-data for parameters of the GPU program used in this pipeline state. */
  119. const SPtr<GpuPipelineParamInfoType>& getParamInfo() const { return mParamInfo; }
  120. protected:
  121. TComputePipelineState();
  122. TComputePipelineState(const GpuProgramType& program);
  123. GpuProgramType mProgram;
  124. SPtr<GpuPipelineParamInfoType> mParamInfo;
  125. };
  126. /** @} */
  127. /** @addtogroup RenderAPI
  128. * @{
  129. */
  130. /**
  131. * Describes the state of the GPU pipeline that determines how are primitives rendered. It consists of programmable
  132. * states (vertex, fragment, geometry, etc. GPU programs), as well as a set of fixed states (blend, rasterizer,
  133. * depth-stencil). Once created the state is immutable, and can be bound to RenderAPI for rendering.
  134. */
  135. class BS_CORE_EXPORT GraphicsPipelineState : public CoreObject, public TGraphicsPipelineState<false>
  136. {
  137. public:
  138. virtual ~GraphicsPipelineState() { }
  139. /**
  140. * Retrieves a core implementation of the pipeline object usable only from the core thread.
  141. *
  142. * @note Core thread only.
  143. */
  144. SPtr<ct::GraphicsPipelineState> getCore() const;
  145. /** @copydoc RenderStateManager::createGraphicsPipelineState */
  146. static SPtr<GraphicsPipelineState> create(const PIPELINE_STATE_DESC& desc);
  147. protected:
  148. friend class RenderStateManager;
  149. GraphicsPipelineState(const PIPELINE_STATE_DESC& desc);
  150. /** @copydoc CoreObject::createCore */
  151. virtual SPtr<ct::CoreObject> createCore() const;
  152. };
  153. /**
  154. * Describes the state of the GPU pipeline that determines how are compute programs executed. It consists of
  155. * of a single programmable state (GPU program). Once created the state is immutable, and can be bound to RenderAPI for
  156. * use.
  157. */
  158. class BS_CORE_EXPORT ComputePipelineState : public CoreObject, public TComputePipelineState<false>
  159. {
  160. public:
  161. virtual ~ComputePipelineState() { }
  162. /**
  163. * Retrieves a core implementation of the pipeline object usable only from the core thread.
  164. *
  165. * @note Core thread only.
  166. */
  167. SPtr<ct::ComputePipelineState> getCore() const;
  168. /** @copydoc RenderStateManager::createComputePipelineState */
  169. static SPtr<ComputePipelineState> create(const SPtr<GpuProgram>& program);
  170. protected:
  171. friend class RenderStateManager;
  172. ComputePipelineState(const SPtr<GpuProgram>& program);
  173. /** @copydoc CoreObject::createCore */
  174. virtual SPtr<ct::CoreObject> createCore() const;
  175. };
  176. /** @} */
  177. namespace ct
  178. {
  179. /** @addtogroup RenderAPI-Internal
  180. * @{
  181. */
  182. /** Core thread version of a bs::GraphicsPipelineState. */
  183. class BS_CORE_EXPORT GraphicsPipelineState : public CoreObject, public TGraphicsPipelineState<true>
  184. {
  185. public:
  186. GraphicsPipelineState(const PIPELINE_STATE_DESC& desc, GpuDeviceFlags deviceMask);
  187. virtual ~GraphicsPipelineState() { }
  188. /** @copydoc CoreObject::initialize() */
  189. void initialize() override;
  190. /** @copydoc RenderStateManager::createGraphicsPipelineState */
  191. static SPtr<GraphicsPipelineState> create(const PIPELINE_STATE_DESC& desc,
  192. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  193. protected:
  194. GpuDeviceFlags mDeviceMask;
  195. };
  196. /** Core thread version of a bs::ComputePipelineState. */
  197. class BS_CORE_EXPORT ComputePipelineState : public CoreObject, public TComputePipelineState<true>
  198. {
  199. public:
  200. ComputePipelineState(const SPtr<GpuProgram>& program, GpuDeviceFlags deviceMask);
  201. virtual ~ComputePipelineState() { }
  202. /** @copydoc CoreObject::initialize() */
  203. void initialize() override;
  204. /** @copydoc RenderStateManager::createComputePipelineState */
  205. static SPtr<ComputePipelineState> create(const SPtr<GpuProgram>& program,
  206. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  207. protected:
  208. GpuDeviceFlags mDeviceMask;
  209. };
  210. /** @} */
  211. }
  212. }