BsShaderVariation.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. namespace bs
  6. {
  7. /** @addtogroup Material-Internal
  8. * @{
  9. */
  10. /** Allows you to specify defines that can control shader compilation. */
  11. class BS_CORE_EXPORT ShaderDefines
  12. {
  13. public:
  14. /** Adds a new define with a floating point value. */
  15. void set(const String& name, float value);
  16. /** Adds a new define with an integer value. */
  17. void set(const String& name, INT32 value);
  18. /** Adds a new define with an integer value. */
  19. void set(const String& name, UINT32 value);
  20. /** Adds a new define with a string point value. */
  21. void set(const String& name, const String& value);
  22. /** Returns a list of all defines. */
  23. UnorderedMap<String, String> getAll() const { return mDefines; }
  24. /** Removes all defines. */
  25. void clear() { mDefines.clear(); }
  26. protected:
  27. UnorderedMap<String, String> mDefines;
  28. };
  29. /**
  30. * Contains information about a single variation of a Shader. Each variation can have a separate set of
  31. * \#defines that control shader compilation.
  32. */
  33. class BS_CORE_EXPORT ShaderVariation : public IReflectable
  34. {
  35. public:
  36. /** Possible types of a variation parameter. */
  37. enum ParamType
  38. {
  39. Int,
  40. UInt,
  41. Float,
  42. Bool
  43. };
  44. /** Name, type and value of a variation parameter. */
  45. struct Param
  46. {
  47. Param()
  48. :i(0), type(Int)
  49. { }
  50. Param(const String& name, INT32 val)
  51. :i(val), name(name), type(Int)
  52. { }
  53. Param(const String& name, UINT32 val)
  54. :ui(val), name(name), type(Int)
  55. { }
  56. Param(const String& name, float val)
  57. :f(val), name(name), type(Float)
  58. { }
  59. Param(const String& name, bool val)
  60. :i(val ? 1 : 0), name(name), type(Bool)
  61. { }
  62. union
  63. {
  64. INT32 i;
  65. UINT32 ui;
  66. float f;
  67. };
  68. StringID name;
  69. ParamType type;
  70. };
  71. ShaderVariation() { }
  72. /** Creates a new shader variation with the specified parameters. */
  73. ShaderVariation(const SmallVector<Param, 4>& params);
  74. /**
  75. * Returns the value of a signed integer parameter with the specified name. Returns 0 if the parameter cannot be
  76. * found.
  77. */
  78. INT32 getInt(const StringID& name);
  79. /**
  80. * Returns the value of a unsigned integer parameter with the specified name. Returns 0 if the parameter cannot be
  81. * found.
  82. */
  83. UINT32 getUInt(const StringID& name);
  84. /** Returns the value of a float parameter with the specified name. Returns 0 if the parameter cannot be found. */
  85. float getFloat(const StringID& name);
  86. /**
  87. * Returns the value of a boolean parameter with the specified name. Returns false if the parameter cannot be
  88. * found.
  89. */
  90. bool getBool(const StringID& name);
  91. /** Converts all the variation parameters in a ShaderDefines object, that may be consumed by the shader compiler. */
  92. ShaderDefines getDefines() const;
  93. /**
  94. * Returns a unique index of this variation, relative to all other variations registered in ShaderVariations object.
  95. */
  96. UINT32 getIdx() const { return mIdx; }
  97. /** Assigns a unique index to the variation that can later be used for quick lookup. */
  98. void setIdx(UINT32 idx) const { mIdx = idx; }
  99. /** Registers a new parameter that controls the variation. */
  100. void addParam(const Param& param) { mParams[param.name] = param; }
  101. /** Returns all the variation parameters. */
  102. const UnorderedMap<StringID, Param>& getParams() const { return mParams; }
  103. bool operator==(const ShaderVariation& rhs) const;
  104. private:
  105. friend class ShaderVariations;
  106. UnorderedMap<StringID, Param> mParams;
  107. mutable UINT32 mIdx = -1;
  108. /************************************************************************/
  109. /* RTTI */
  110. /************************************************************************/
  111. public:
  112. friend class ShaderVariationRTTI;
  113. static RTTITypeBase* getRTTIStatic();
  114. RTTITypeBase* getRTTI() const override;
  115. };
  116. /** A container for all variations of a single Shader. */
  117. class BS_CORE_EXPORT ShaderVariations
  118. {
  119. public:
  120. /** Registers a new variation. */
  121. void add(const ShaderVariation& variation);
  122. /** Returns a variation at the specified index. Variations are indexed sequentially as they are added. */
  123. const ShaderVariation& get(UINT32 idx) { return mVariations[idx]; }
  124. /**
  125. * Scans a list of stored variations and returns an index of a variation that has the same parameters as the
  126. * provided one, or -1 if one is not found.
  127. */
  128. UINT32 find(const ShaderVariation& variation) const;
  129. /** Returns a list of all variations. */
  130. const SmallVector<ShaderVariation, 4>& getVariations() const { return mVariations; }
  131. private:
  132. SmallVector<ShaderVariation, 4> mVariations;
  133. UINT32 mNextIdx = 0;
  134. };
  135. /** @} */
  136. }