BsGpuPipelineState.h 8.6 KB

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