BsPass.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "Image/BsColor.h"
  6. #include "Reflection/BsIReflectable.h"
  7. #include "CoreThread/BsCoreObject.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Material
  11. * @{
  12. */
  13. /** Descriptor structure used for initializing a shader pass. */
  14. struct PASS_DESC
  15. {
  16. SPtr<BlendState> blendState;
  17. SPtr<RasterizerState> rasterizerState;
  18. SPtr<DepthStencilState> depthStencilState;
  19. UINT32 stencilRefValue;
  20. SPtr<GpuProgram> vertexProgram;
  21. SPtr<GpuProgram> fragmentProgram;
  22. SPtr<GpuProgram> geometryProgram;
  23. SPtr<GpuProgram> hullProgram;
  24. SPtr<GpuProgram> domainProgram;
  25. SPtr<GpuProgram> computeProgram;
  26. };
  27. /** @} */
  28. namespace ct
  29. {
  30. /** @addtogroup Material-Internal
  31. * @{
  32. */
  33. /** Descriptor structure used for initializing a core thread variant of a shader pass. */
  34. struct PASS_DESC
  35. {
  36. SPtr<BlendState> blendState;
  37. SPtr<RasterizerState> rasterizerState;
  38. SPtr<DepthStencilState> depthStencilState;
  39. UINT32 stencilRefValue;
  40. SPtr<GpuProgram> vertexProgram;
  41. SPtr<GpuProgram> fragmentProgram;
  42. SPtr<GpuProgram> geometryProgram;
  43. SPtr<GpuProgram> hullProgram;
  44. SPtr<GpuProgram> domainProgram;
  45. SPtr<GpuProgram> computeProgram;
  46. };
  47. /** @} */
  48. }
  49. /** @addtogroup Implementation
  50. * @{
  51. */
  52. /** Contains all data used by a pass, templated so it may contain both core and sim thread data. */
  53. template<bool Core>
  54. struct TPassTypes
  55. { };
  56. template<>
  57. struct TPassTypes < false >
  58. {
  59. typedef SPtr<BlendState> BlendStateType;
  60. typedef SPtr<RasterizerState> RasterizerStateType;
  61. typedef SPtr<DepthStencilState> DepthStencilStateType;
  62. typedef SPtr<GpuProgram> GpuProgramType;
  63. typedef SPtr<GraphicsPipelineState> GraphicsPipelineStateType;
  64. typedef SPtr<ComputePipelineState> ComputePipelineStateType;
  65. typedef PASS_DESC PassDescType;
  66. };
  67. template<>
  68. struct TPassTypes < true >
  69. {
  70. typedef SPtr<ct::BlendState> BlendStateType;
  71. typedef SPtr<ct::RasterizerState> RasterizerStateType;
  72. typedef SPtr<ct::DepthStencilState> DepthStencilStateType;
  73. typedef SPtr<ct::GpuProgram> GpuProgramType;
  74. typedef SPtr<ct::GraphicsPipelineState> GraphicsPipelineStateType;
  75. typedef SPtr<ct::ComputePipelineState> ComputePipelineStateType;
  76. typedef ct::PASS_DESC PassDescType;
  77. };
  78. /**
  79. * Class defining a single pass of a technique (of a material).
  80. *
  81. * Pass may contain multiple GPU programs (vertex, fragment, geometry, etc.), and a set of pipeline states (blend,
  82. * rasterizer, etc.).
  83. *
  84. * @note Templated so it can be used for both core and non-core versions of a pass.
  85. */
  86. template<bool Core>
  87. class BS_CORE_EXPORT TPass
  88. {
  89. public:
  90. typedef typename TPassTypes<Core>::BlendStateType BlendStateType;
  91. typedef typename TPassTypes<Core>::RasterizerStateType RasterizerStateType;
  92. typedef typename TPassTypes<Core>::DepthStencilStateType DepthStencilStateType;
  93. typedef typename TPassTypes<Core>::GpuProgramType GpuProgramType;
  94. typedef typename TPassTypes<Core>::PassDescType PassDescType;
  95. typedef typename TPassTypes<Core>::GraphicsPipelineStateType GraphicsPipelineStateType;
  96. typedef typename TPassTypes<Core>::ComputePipelineStateType ComputePipelineStateType;
  97. virtual ~TPass() { }
  98. bool hasVertexProgram() const { return mData.vertexProgram != nullptr; }
  99. bool hasFragmentProgram() const { return mData.fragmentProgram != nullptr; }
  100. bool hasGeometryProgram() const { return mData.geometryProgram != nullptr; }
  101. bool hasHullProgram() const { return mData.hullProgram != nullptr; }
  102. bool hasDomainProgram() const { return mData.domainProgram != nullptr; }
  103. bool hasComputeProgram() const { return mData.computeProgram != nullptr; }
  104. /** Returns true if this pass has some element of transparency. */
  105. bool hasBlending() const;
  106. BlendStateType getBlendState() const { return mData.blendState; }
  107. RasterizerStateType getRasterizerState() const { return mData.rasterizerState; }
  108. DepthStencilStateType getDepthStencilState() const { return mData.depthStencilState; }
  109. /** Gets the stencil reference value that is used when performing operations using the stencil buffer. */
  110. UINT32 getStencilRefValue() const { return mData.stencilRefValue; }
  111. const GpuProgramType& getVertexProgram() const { return mData.vertexProgram; }
  112. const GpuProgramType& getFragmentProgram() const { return mData.fragmentProgram; }
  113. const GpuProgramType& getGeometryProgram() const { return mData.geometryProgram; }
  114. const GpuProgramType& getHullProgram() const { return mData.hullProgram; }
  115. const GpuProgramType& getDomainProgram() const { return mData.domainProgram; }
  116. const GpuProgramType& getComputeProgram() const { return mData.computeProgram; }
  117. /** Returns the graphics pipeline state describing this pass, or null if its a compute pass. */
  118. const GraphicsPipelineStateType& getGraphicsPipelineState() const { return mGraphicsPipelineState; }
  119. /** Returns the compute pipeline state describing this pass, or null if its a graphics pass. */
  120. const ComputePipelineStateType& getComputePipelineState() const { return mComputePipelineState; }
  121. protected:
  122. TPass();
  123. TPass(const PassDescType& desc);
  124. PassDescType mData;
  125. GraphicsPipelineStateType mGraphicsPipelineState;
  126. ComputePipelineStateType mComputePipelineState;
  127. };
  128. /** @} */
  129. /** @addtogroup Material
  130. * @{
  131. */
  132. /**
  133. * @copydoc TPass
  134. *
  135. * @note Sim thread.
  136. */
  137. class BS_CORE_EXPORT Pass : public IReflectable, public CoreObject, public TPass<false>
  138. {
  139. public:
  140. virtual ~Pass() { }
  141. /** Retrieves an implementation of a pass usable only from the core thread. */
  142. SPtr<ct::Pass> getCore() const;
  143. /** Creates a new empty pass. */
  144. static SPtr<Pass> create(const PASS_DESC& desc);
  145. protected:
  146. friend class Technique;
  147. Pass() { }
  148. Pass(const PASS_DESC& desc);
  149. /** @copydoc CoreObject::initialize */
  150. void initialize() override;
  151. /** @copydoc CoreObject::syncToCore */
  152. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  153. /** @copydoc CoreObject::createCore */
  154. SPtr<ct::CoreObject> createCore() const override;
  155. /** @copydoc CoreObject::syncToCore */
  156. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  157. /** Creates a new empty pass but doesn't initialize it. */
  158. static SPtr<Pass> createEmpty();
  159. /************************************************************************/
  160. /* RTTI */
  161. /************************************************************************/
  162. public:
  163. friend class PassRTTI;
  164. static RTTITypeBase* getRTTIStatic();
  165. RTTITypeBase* getRTTI() const override;
  166. };
  167. /** @} */
  168. namespace ct
  169. {
  170. /** @addtogroup Material-Internal
  171. * @{
  172. */
  173. /**
  174. * @copydoc TPass
  175. *
  176. * @note Core thread.
  177. */
  178. class BS_CORE_EXPORT Pass : public CoreObject, public TPass<true>
  179. {
  180. public:
  181. virtual ~Pass() { }
  182. /** Creates a new empty pass. */
  183. static SPtr<Pass> create(const PASS_DESC& desc);
  184. protected:
  185. friend class bs::Pass;
  186. friend class Technique;
  187. /** @copydoc CoreObject::initialize */
  188. void initialize() override;
  189. Pass() { }
  190. Pass(const PASS_DESC& desc);
  191. Pass(const PASS_DESC& desc, const SPtr<GraphicsPipelineState>& pipelineState);
  192. Pass(const PASS_DESC& desc, const SPtr<ComputePipelineState>& pipelineState);
  193. /** @copydoc CoreObject::syncToCore */
  194. void syncToCore(const CoreSyncData& data) override;
  195. };
  196. /** @} */
  197. }
  198. }