compiler.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifndef __BASIC_SHADER_LOADER_H__
  2. #define __BASIC_SHADER_LOADER_H__
  3. #include <irrlicht/irrlicht.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <map>
  7. #include <nlohmann/json.hpp>
  8. #include <WallpaperEngine/FileSystem/utils.h>
  9. namespace WallpaperEngine
  10. {
  11. namespace shaders
  12. {
  13. using json = nlohmann::json;
  14. /**
  15. * A basic shader loader that adds basic function definitions to every loaded shader
  16. */
  17. class compiler
  18. {
  19. public:
  20. /**
  21. * Basic struct used to define all the shader variables
  22. * the compiler will replace in pre-processing time
  23. * to make sure the shaders compile under OpenGL
  24. */
  25. struct VariableReplacement
  26. {
  27. const char* original;
  28. const char* replacement;
  29. };
  30. struct TypeName
  31. {
  32. const char* name;
  33. int size;
  34. };
  35. struct ShaderParameter
  36. {
  37. std::string type;
  38. std::string variableName;
  39. std::string identifierName;
  40. void* defaultValue;
  41. void* range [2];
  42. };
  43. /**
  44. * Types of shaders
  45. */
  46. enum Type
  47. {
  48. Type_Vertex = 0,
  49. Type_Pixel = 1,
  50. };
  51. /**
  52. * List of variables to replace when pre-process is performed
  53. */
  54. static std::map<std::string, std::string> sVariableReplacement;
  55. /**
  56. * Types of variables the pre-processor understands
  57. */
  58. static std::vector<std::string> sTypes;
  59. /**
  60. * Compiler constructor, loads the given shader file and prepares
  61. * the pre-processing and compilation of the shader, adding
  62. * required definitions if needed
  63. *
  64. * @param file The file to load
  65. * @param type The type of shader
  66. * @param recursive Whether the compiler should add base definitions or not
  67. */
  68. compiler (irr::io::path& file, Type type, std::map<std::string, int>* combos, bool recursive = false);
  69. /**
  70. * Performs the actual pre-compilation/pre-processing over the shader files
  71. * This step is kinda big, replaces variables names on sVariableReplacement,
  72. * ensures #include directives are correctly handled
  73. * and takes care of attribute comments for the wallpaper engine specifics
  74. *
  75. * @return The shader contents ready to be used by OpenGL
  76. */
  77. std::string precompile ();
  78. /**
  79. * Searches the list of parameters available for the parameter with the given value
  80. *
  81. * @param identifier The identifier to search for
  82. * @return The shader information
  83. */
  84. ShaderParameter* findParameter (std::string identifier);
  85. /**
  86. * @return The list of parameters available for this shader with their default values
  87. */
  88. std::vector <ShaderParameter*>& getParameters ();
  89. private:
  90. /**
  91. * Checks if there is "str" in the current position without advancing the
  92. * iterator in use
  93. *
  94. * @param str The string to check for
  95. * @param it The position to start checking at
  96. *
  97. * @return
  98. */
  99. bool peekString (std::string str, std::string::const_iterator& it);
  100. /**
  101. * Checks for a semicolon as current character, advancing the iterator
  102. * after finding it, otherwise returns an error
  103. *
  104. * @param it The position where to expect the semicolon
  105. *
  106. * @return
  107. */
  108. bool expectSemicolon (std::string::const_iterator& it);
  109. /**
  110. * Ignores contiguous space characters in the string advancing the iterator
  111. * until the first non-space character
  112. *
  113. * @param it The iterator to increase
  114. */
  115. void ignoreSpaces (std::string::const_iterator& it);
  116. /**
  117. * Ignores all characters until next line-fee (\n) advancing the interator
  118. *
  119. * @param it The iterator to increase
  120. */
  121. void ignoreUpToNextLineFeed (std::string::const_iterator& it);
  122. /**
  123. * Ignores all characters until a block comment end is found, advancing the iterator
  124. *
  125. * @param it The iterator to increase
  126. */
  127. void ignoreUpToBlockCommentEnd (std::string::const_iterator& it);
  128. /**
  129. * Parses the current position as a variable type, extracts it and compares it
  130. * to the registered types in the pre-processor, returning it's name if valid
  131. * increasing the iterator at the same time
  132. *
  133. * @param it The position to extract it from
  134. *
  135. * @return The type name
  136. */
  137. std::string extractType (std::string::const_iterator& it);
  138. /**
  139. * Parses the current position as a variable name, extractig it's name and
  140. * increasing the iterator as the name is extracted
  141. *
  142. * @param it The position to start extracting the variable name from
  143. *
  144. * @return The variable name
  145. */
  146. std::string extractName (std::string::const_iterator& it);
  147. /**
  148. * Parses the current position as a quoted value, extracting it's value
  149. * and increasing the iterator at the same time
  150. *
  151. * @param it The position to start extracting the value from
  152. *
  153. * @return The value
  154. */
  155. std::string extractQuotedValue (std::string::const_iterator& it);
  156. /**
  157. * Tries to find the given shader file and compile it
  158. *
  159. * @param filename The shader's filename
  160. *
  161. * @return The compiled contents
  162. */
  163. std::string lookupShaderFile (std::string filename);
  164. /**
  165. * Searches for the given symbol in the replace table
  166. *
  167. * @param symbol The symbol to look for
  168. *
  169. * @return The symbol it should be replaced with
  170. */
  171. std::string lookupReplaceSymbol (std::string symbol);
  172. /**
  173. * @return Whether the character in the current position is a character or not
  174. */
  175. bool isChar (std::string::const_iterator& it);
  176. /**
  177. * @return Whether the character in the current position is a number or not
  178. */
  179. bool isNumeric (std::string::const_iterator& it);
  180. /**
  181. * Parses a COMBO value to add the proper define to the code
  182. *
  183. * @param content The parameter configuration
  184. */
  185. void parseComboConfiguration (const std::string& content);
  186. /**
  187. * Parses a parameter extra metadata created by wallpaper engine
  188. *
  189. * @param type The type of variable to parse
  190. * @param name The name of the variable in the shader (for actual variable declaration)
  191. * @param content The parameter configuration
  192. */
  193. void parseParameterConfiguration (const std::string& type, const std::string& name, const std::string& content);
  194. /**
  195. * The shader file this instance is loading
  196. */
  197. irr::io::path m_file;
  198. /**
  199. * The original file content
  200. */
  201. std::string m_content;
  202. /**
  203. * The final, compiled content ready to be used by OpenGL
  204. */
  205. std::string m_compiledContent;
  206. /**
  207. * Whether there was any kind of error in the compilation or not
  208. */
  209. bool m_error;
  210. /**
  211. * Extra information about the error (if any)
  212. */
  213. std::string m_errorInfo;
  214. /**
  215. * The type of shader
  216. */
  217. Type m_type;
  218. /**
  219. * The parameters the shader needs
  220. */
  221. std::vector <ShaderParameter*> m_parameters;
  222. /**
  223. * The combos the shader should be generated with
  224. */
  225. std::map <std::string, int>* m_combos;
  226. /**
  227. * Whether this compilation is a recursive one or not
  228. */
  229. bool m_recursive;
  230. };
  231. }
  232. }
  233. #endif /* !__BASIC_SHADER_LOADER_H__ */