ShaderUnit.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #include <map>
  3. #include <memory>
  4. #include <string>
  5. #include "GLSLContext.h"
  6. #include "WallpaperEngine/Assets/AssetLocator.h"
  7. #include "WallpaperEngine/Data/JSON.h"
  8. #include "WallpaperEngine/Render/Shaders/Variables/ShaderVariable.h"
  9. #include "nlohmann/json.hpp"
  10. #include "WallpaperEngine/Data/Model/Types.h"
  11. namespace WallpaperEngine::Render::Shaders {
  12. using JSON = WallpaperEngine::Data::JSON::JSON;
  13. using namespace WallpaperEngine::Assets;
  14. using namespace WallpaperEngine::Data::Model;
  15. /**
  16. * Represents a whole shader unit
  17. */
  18. class ShaderUnit {
  19. public:
  20. ShaderUnit (
  21. const GLSLContext::UnitType type, std::string file, std::string content, const AssetLocator& assetLocator,
  22. const ShaderConstantMap& constants, const TextureMap& passTextures, const TextureMap& overrideTextures,
  23. const ComboMap& combos, const ComboMap& overrideCombos
  24. );
  25. ~ShaderUnit () = default;
  26. /**
  27. * Links this shader unit with another unit so they're treated as one
  28. *
  29. * @param unit
  30. */
  31. void linkToUnit (const ShaderUnit* unit);
  32. /**
  33. * @return The shader unit linked to this unit (if any)
  34. */
  35. [[nodiscard]] const ShaderUnit* getLinkedUnit () const;
  36. /**
  37. * @return The unit's source code already compiled and ready to be used by OpenGL
  38. */
  39. [[nodiscard]] const std::string& compile ();
  40. /**
  41. * @return The parameters the shader unit has as input
  42. */
  43. [[nodiscard]] const std::vector<Variables::ShaderVariable*>& getParameters () const;
  44. /**
  45. * @return The textures this shader unit requires
  46. */
  47. [[nodiscard]] const TextureMap& getTextures () const;
  48. /**
  49. * @return The combos set for this shader unit by the configuration
  50. */
  51. [[nodiscard]] const ComboMap& getCombos () const;
  52. /**
  53. * @return Other combos detected by this shader unit during the preprocess
  54. */
  55. [[nodiscard]] const ComboMap& getDiscoveredCombos () const;
  56. protected:
  57. /**
  58. * Extracts any and all possible shader combo configurations
  59. * available in this shader unit, prepares includes
  60. * and lays the ground for the actual code to be ready
  61. */
  62. void preprocess ();
  63. private:
  64. /**
  65. * Parses the input shader looking for possible combo values that are required for it to properly work
  66. */
  67. void preprocessVariables ();
  68. /**
  69. * Parses the input shader looking for include directives to extract the full list of included files
  70. */
  71. void preprocessIncludes ();
  72. /**
  73. * Parses the input shader looking for require directives and resolves them into generated code
  74. */
  75. void preprocessRequires ();
  76. /**
  77. * Resolves a #require module name to generated GLSL code
  78. *
  79. * @param moduleName The module to resolve (e.g. "LightingV1")
  80. * @return Generated GLSL code for the module, or empty string if unknown
  81. */
  82. [[nodiscard]] std::string resolveRequireModule (const std::string& moduleName) const;
  83. /**
  84. * Generates the LightingV1 module stub (PerformLighting_V1 function)
  85. *
  86. * @return GLSL code defining PerformLighting_V1
  87. */
  88. [[nodiscard]] std::string generateLightingV1 () const;
  89. /**
  90. * Adjusts vertex varyings when a workshop shader declares a narrower vertex type than its fragment peer.
  91. */
  92. [[nodiscard]] std::string applyLinkedVaryingCompatibility (std::string source) const;
  93. /**
  94. * Adjusts fragment shaders that use wide texture coordinates as vec2 values in Wallpaper Engine effects.
  95. */
  96. [[nodiscard]] std::string applyFragmentTexCoordCompatibility (std::string source) const;
  97. /**
  98. * Parses a COMBO value to add the proper define to the code
  99. *
  100. * @param content The parameter configuration
  101. * @param defaultValue
  102. */
  103. void parseComboConfiguration (const std::string& content, int defaultValue = 0);
  104. /**
  105. * Parses a parameter extra metadata created by wallpaper engine
  106. *
  107. * @param type The type of variable to parse
  108. * @param name The name of the variable in the shader (for actual variable declaration)
  109. * @param content The parameter configuration
  110. */
  111. void parseParameterConfiguration (const std::string& type, const std::string& name, const std::string& content);
  112. /**
  113. * The type of shder unit we have
  114. */
  115. GLSLContext::UnitType m_type;
  116. /**
  117. * The filename of this shader unit
  118. */
  119. std::string m_file;
  120. /**
  121. * Shader's original contents
  122. */
  123. std::string m_content;
  124. /**
  125. * Includes content to be added on compilation
  126. */
  127. std::string m_includes;
  128. /**
  129. * Shader's content after the preprocessing step
  130. */
  131. std::string m_preprocessed;
  132. /**
  133. * Shader's code after the compilation of glslang and spirv
  134. */
  135. std::string m_final;
  136. /**
  137. * The parameters the shader needs
  138. */
  139. std::vector<Variables::ShaderVariable*> m_parameters = {};
  140. /**
  141. * Pre-defined values for the combos
  142. */
  143. const ComboMap& m_combos;
  144. /**
  145. * Pre-defined overriden values for the combos
  146. */
  147. const ComboMap& m_overrideCombos;
  148. /**
  149. * The combos discovered in the pre-processing step that were not in the combos list
  150. */
  151. ComboMap m_discoveredCombos = {};
  152. /**
  153. * The combos used by this unit that should be added
  154. */
  155. std::map<std::string, bool> m_usedCombos = {};
  156. /**
  157. * The constants defined for this unit
  158. */
  159. const ShaderConstantMap& m_constants;
  160. /** The textures that are already applied to this shader */
  161. const TextureMap& m_passTextures;
  162. /** The textures that are being overridden */
  163. const TextureMap& m_overrideTextures;
  164. /** The default textures to use when a texture is not applied in a given slot */
  165. TextureMap m_defaultTextures = {};
  166. /**
  167. * The shader unit this unit is linked to
  168. */
  169. const ShaderUnit* m_link;
  170. /**
  171. * The container to source files from
  172. */
  173. const AssetLocator& m_assetLocator;
  174. };
  175. }