BsPass.h 5.9 KB

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