BsPass.h 6.6 KB

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