compiler.h 8.7 KB

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