BsPass.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 BansheeEngine
  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<GpuPipelineState> GpuPipelineStateType;
  61. typedef PASS_DESC PassDescType;
  62. };
  63. template<>
  64. struct TPassTypes < true >
  65. {
  66. typedef SPtr<BlendStateCore> BlendStateType;
  67. typedef SPtr<RasterizerStateCore> RasterizerStateType;
  68. typedef SPtr<DepthStencilStateCore> DepthStencilStateType;
  69. typedef SPtr<GpuProgramCore> GpuProgramType;
  70. typedef SPtr<GpuPipelineStateCore> GpuPipelineStateType;
  71. typedef PASS_DESC_CORE PassDescType;
  72. };
  73. /**
  74. * Class defining a single pass of a technique (of a material).
  75. *
  76. * Pass may contain multiple GPU programs (vertex, fragment, geometry, etc.), and a set of pipeline states (blend,
  77. * rasterizer, etc.).
  78. *
  79. * @note Templated so it can be used for both core and non-core versions of a pass.
  80. */
  81. template<bool Core>
  82. class BS_CORE_EXPORT TPass
  83. {
  84. public:
  85. typedef typename TPassTypes<Core>::BlendStateType BlendStateType;
  86. typedef typename TPassTypes<Core>::RasterizerStateType RasterizerStateType;
  87. typedef typename TPassTypes<Core>::DepthStencilStateType DepthStencilStateType;
  88. typedef typename TPassTypes<Core>::GpuProgramType GpuProgramType;
  89. typedef typename TPassTypes<Core>::PassDescType PassDescType;
  90. typedef typename TPassTypes<Core>::GpuPipelineStateType GpuPipelineStateType;
  91. virtual ~TPass() { }
  92. bool hasVertexProgram() const { return mData.vertexProgram != nullptr; }
  93. bool hasFragmentProgram() const { return mData.fragmentProgram != nullptr; }
  94. bool hasGeometryProgram() const { return mData.geometryProgram != nullptr; }
  95. bool hasHullProgram() const { return mData.hullProgram != nullptr; }
  96. bool hasDomainProgram() const { return mData.domainProgram != nullptr; }
  97. bool hasComputeProgram() const { return mData.computeProgram != nullptr; }
  98. /** Returns true if this pass has some element of transparency. */
  99. bool hasBlending() const;
  100. BlendStateType getBlendState() const { return mData.blendState; }
  101. RasterizerStateType getRasterizerState() const { return mData.rasterizerState; }
  102. DepthStencilStateType getDepthStencilState() const { return mData.depthStencilState; }
  103. /** Gets the stencil reference value that is used when performing operations using the stencil buffer. */
  104. UINT32 getStencilRefValue() const { return mData.stencilRefValue; }
  105. const GpuProgramType& getVertexProgram() const { return mData.vertexProgram; }
  106. const GpuProgramType& getFragmentProgram() const { return mData.fragmentProgram; }
  107. const GpuProgramType& getGeometryProgram() const { return mData.geometryProgram; }
  108. const GpuProgramType& getHullProgram() const { return mData.hullProgram; }
  109. const GpuProgramType& getDomainProgram() const { return mData.domainProgram; }
  110. const GpuProgramType& getComputeProgram() const { return mData.computeProgram; }
  111. const GpuPipelineStateType& getPipelineState() const { return mPipelineState; }
  112. protected:
  113. TPass();
  114. TPass(const PassDescType& desc);
  115. PassDescType mData;
  116. GpuPipelineStateType mPipelineState;
  117. };
  118. /** @} */
  119. /** @addtogroup Material-Internal
  120. * @{
  121. */
  122. /**
  123. * @copydoc TPass
  124. *
  125. * @note Core thread.
  126. */
  127. class BS_CORE_EXPORT PassCore : public CoreObjectCore, public TPass<true>
  128. {
  129. public:
  130. virtual ~PassCore() { }
  131. /** Creates a new empty pass. */
  132. static SPtr<PassCore> create(const PASS_DESC_CORE& desc);
  133. protected:
  134. friend class Pass;
  135. friend class TechniqueCore;
  136. /** @copydoc CoreObjectCore::initialize */
  137. void initialize() override;
  138. PassCore() { }
  139. PassCore(const PASS_DESC_CORE& desc);
  140. PassCore(const PASS_DESC_CORE& desc, const SPtr<GpuPipelineStateCore>& pipelineState);
  141. /** @copydoc CoreObjectCore::syncToCore */
  142. void syncToCore(const CoreSyncData& data) override;
  143. };
  144. /** @} */
  145. /** @addtogroup Material
  146. * @{
  147. */
  148. /**
  149. * @copydoc TPass
  150. *
  151. * @note Sim thread.
  152. */
  153. class BS_CORE_EXPORT Pass : public IReflectable, public CoreObject, public TPass<false>
  154. {
  155. public:
  156. virtual ~Pass() { }
  157. /** Retrieves an implementation of a pass usable only from the core thread. */
  158. SPtr<PassCore> getCore() const;
  159. /** Creates a new empty pass. */
  160. static SPtr<Pass> create(const PASS_DESC& desc);
  161. protected:
  162. friend class Technique;
  163. Pass() { }
  164. Pass(const PASS_DESC& desc);
  165. /** @copydoc CoreObject::initialize */
  166. void initialize() override;
  167. /** @copydoc CoreObject::syncToCore */
  168. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  169. /** @copydoc CoreObject::createCore */
  170. SPtr<CoreObjectCore> createCore() const override;
  171. /** @copydoc CoreObject::syncToCore */
  172. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  173. /** Creates a new empty pass but doesn't initialize it. */
  174. static SPtr<Pass> createEmpty();
  175. /************************************************************************/
  176. /* RTTI */
  177. /************************************************************************/
  178. public:
  179. friend class PassRTTI;
  180. static RTTITypeBase* getRTTIStatic();
  181. RTTITypeBase* getRTTI() const override;
  182. };
  183. /** @} */
  184. }