BsPass.h 7.5 KB

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