BsTechnique.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "Reflection/BsIReflectable.h"
  6. #include "CoreThread/BsCoreObject.h"
  7. #include "Material/BsShaderVariation.h"
  8. #include "String/BsStringID.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Implementation
  12. * @{
  13. */
  14. /** Base class that is used for implementing both sim and core versions of Technique. */
  15. class BS_CORE_EXPORT TechniqueBase
  16. {
  17. public:
  18. TechniqueBase(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  19. const ShaderVariation& variation);
  20. virtual ~TechniqueBase() { }
  21. /** Checks if this technique is supported based on current render and other systems. */
  22. bool isSupported() const;
  23. /** Checks if the technique has the specified tag. */
  24. bool hasTag(const StringID& tag);
  25. /** Checks if the technique has any tags. */
  26. UINT32 hasTags() const { return !mTags.empty(); }
  27. /** Returns a set of preprocessor defines used for compiling this particular technique. */
  28. const ShaderVariation& getVariation() const { return mVariation; }
  29. protected:
  30. String mLanguage;
  31. StringID mRenderer;
  32. Vector<StringID> mTags;
  33. ShaderVariation mVariation;
  34. };
  35. template<bool Core> struct TPassType { };
  36. template<> struct TPassType < false > { typedef Pass Type; };
  37. template<> struct TPassType < true > { typedef ct::Pass Type; };
  38. template<bool Core> struct TTechniqueType {};
  39. template<> struct TTechniqueType < false > { typedef Technique Type; };
  40. template<> struct TTechniqueType < true > { typedef ct::Technique Type; };
  41. /** Templated class that is used for implementing both sim and core versions of Technique. */
  42. template<bool Core>
  43. class BS_CORE_EXPORT TTechnique : public TechniqueBase
  44. {
  45. public:
  46. typedef typename TPassType<Core>::Type PassType;
  47. TTechnique();
  48. TTechnique(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  49. const ShaderVariation& variation, const Vector<SPtr<PassType>>& passes);
  50. virtual ~TTechnique() { }
  51. /** Returns a pass with the specified index. */
  52. SPtr<PassType> getPass(UINT32 idx) const;
  53. /** Returns total number of passes. */
  54. UINT32 getNumPasses() const { return (UINT32)mPasses.size(); }
  55. protected:
  56. Vector<SPtr<PassType>> mPasses;
  57. };
  58. /** @} */
  59. /** @addtogroup Material
  60. * @{
  61. */
  62. /**
  63. * Technique is a set of shading passes bindable to the GPU pipeline. Each technique can also have a set of properties
  64. * that help the engine to determine which technique should be used under which circumstances (if more than one
  65. * technique is available).
  66. *
  67. * @note
  68. * Normally you want to have a separate technique for every render system and renderer your application supports.
  69. * For example, if you are supporting DirectX11 and OpenGL you will want to have two techniques, one using HLSL based
  70. * GPU programs, other using GLSL. Those techniques should try to mirror each other's end results.
  71. */
  72. class BS_CORE_EXPORT Technique : public IReflectable, public CoreObject, public TTechnique<false>
  73. {
  74. public:
  75. Technique(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  76. const ShaderVariation& variation, const Vector<SPtr<Pass>>& passes);
  77. /** Retrieves an implementation of a technique usable only from the core thread. */
  78. SPtr<ct::Technique> getCore() const;
  79. /**
  80. * Creates a new technique.
  81. *
  82. * @param[in] language Shading language used by the technique. The engine will not use this technique unless
  83. * this language is supported by the render API.
  84. * @param[in] renderer Renderer the technique supports. Under normal circumstances the engine will not use
  85. * this technique unless this renderer is enabled.
  86. * @param[in] passes A set of passes that define the technique.
  87. * @return Newly creted technique.
  88. */
  89. static SPtr<Technique> create(const String& language, const StringID& renderer, const Vector<SPtr<Pass>>& passes);
  90. /**
  91. * Creates a new technique.
  92. *
  93. * @param[in] language Shading language used by the technique. The engine will not use this technique unless
  94. * this language is supported by the render API.
  95. * @param[in] renderer Renderer the technique supports. Under normal circumstances the engine will not use
  96. * this technique unless this renderer is enabled.
  97. * @param[in] tags An optional set of tags that can be used for further identifying under which
  98. * circumstances should a technique be used.
  99. * @param[in] variation A set of preprocessor directives that were used for compiling this particular technique.
  100. * Used for shaders that have multiple variations.
  101. * @param[in] passes A set of passes that define the technique.
  102. * @return Newly creted technique.
  103. */
  104. static SPtr<Technique> create(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  105. const ShaderVariation& variation, const Vector<SPtr<Pass>>& passes);
  106. protected:
  107. /** @copydoc CoreObject::createCore */
  108. SPtr<ct::CoreObject> createCore() const override;
  109. /** @copydoc CoreObject::getCoreDependencies */
  110. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  111. /** Creates a new technique but doesn't initialize it. */
  112. static SPtr<Technique> createEmpty();
  113. private:
  114. /************************************************************************/
  115. /* RTTI */
  116. /************************************************************************/
  117. /** Serialization only constructor. */
  118. Technique();
  119. public:
  120. friend class TechniqueRTTI;
  121. static RTTITypeBase* getRTTIStatic();
  122. RTTITypeBase* getRTTI() const override;
  123. };
  124. /** @} */
  125. namespace ct
  126. {
  127. /** @addtogroup Material-Internal
  128. * @{
  129. */
  130. /** Core thread version of bs::Technique. */
  131. class BS_CORE_EXPORT Technique : public CoreObject, public TTechnique<true>
  132. {
  133. public:
  134. Technique(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  135. const ShaderVariation& variation, const Vector<SPtr<Pass>>& passes);
  136. /** @copydoc bs::Technique::create(const String&, const StringID&, const Vector<SPtr<Pass>>&) */
  137. static SPtr<Technique> create(const String& language, const StringID& renderer,
  138. const Vector<SPtr<Pass>>& passes);
  139. /**
  140. * @copydoc bs::Technique::create(const String&, const StringID&, const Vector<StringID>&,
  141. * const ShaderVariation&, const Vector<SPtr<Pass>>&)
  142. */
  143. static SPtr<Technique> create(const String& language, const StringID& renderer, const Vector<StringID>& tags,
  144. const ShaderVariation& variation, const Vector<SPtr<Pass>>& passes);
  145. };
  146. /** @} */
  147. }
  148. }