BsTechnique.h 6.1 KB

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