BsTechnique.h 6.7 KB

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