compiler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/fileResolver.h>
  8. namespace wp
  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. /**
  34. * Types of shaders
  35. */
  36. enum Type
  37. {
  38. Type_Vertex = 0,
  39. Type_Pixel = 1,
  40. };
  41. /**
  42. * List of variables to replace when pre-process is performed
  43. */
  44. static std::map<std::string, std::string> sVariableReplacement;
  45. /**
  46. * Types of variables the pre-processor understands
  47. */
  48. static std::vector<std::string> sTypes;
  49. /**
  50. * Compiler constructor, loads the given shader file and prepares
  51. * the pre-processing and compilation of the shader, adding
  52. * required definitions if needed
  53. *
  54. * @param file The file to load
  55. * @param type The type of shader
  56. * @param recursive Whether the compiler should add base definitions or not
  57. */
  58. compiler (irr::io::path file, Type type, bool recursive = false);
  59. /**
  60. * Performs the actual pre-compilation/pre-processing over the shader files
  61. * This step is kinda big, replaces variables names on sVariableReplacement,
  62. * ensures #include directives are correctly handled
  63. * and takes care of attribute comments for the wallpaper engine specifics
  64. *
  65. * @return The shader contents ready to be used by OpenGL
  66. */
  67. std::string precompile();
  68. private:
  69. /**
  70. * Checks if there is "str" in the current position without advancing the
  71. * iterator in use
  72. *
  73. * @param str The string to check for
  74. * @param it The position to start checking at
  75. *
  76. * @return
  77. */
  78. bool peekString (std::string str, std::string::const_iterator& it);
  79. /**
  80. * Checks for a semicolon as current character, advancing the iterator
  81. * after finding it, otherwise returns an error
  82. *
  83. * @param it The position where to expect the semicolon
  84. *
  85. * @return
  86. */
  87. bool expectSemicolon (std::string::const_iterator& it);
  88. /**
  89. * Ignores contiguous space characters in the string advancing the iterator
  90. * until the first non-space character
  91. *
  92. * @param it The iterator to increase
  93. */
  94. void ignoreSpaces (std::string::const_iterator& it);
  95. /**
  96. * Ignores all characters until next line-fee (\n) advancing the interator
  97. *
  98. * @param it The iterator to increase
  99. */
  100. void ignoreUpToNextLineFeed (std::string::const_iterator& it);
  101. /**
  102. * Ignores all characters until a block comment end is found, advancing the iterator
  103. *
  104. * @param it The iterator to increase
  105. */
  106. void ignoreUpToBlockCommentEnd (std::string::const_iterator& it);
  107. /**
  108. * Parses the current position as a variable type, extracts it and compares it
  109. * to the registered types in the pre-processor, returning it's name if valid
  110. * increasing the iterator at the same time
  111. *
  112. * @param it The position to extract it from
  113. *
  114. * @return The type name
  115. */
  116. std::string extractType (std::string::const_iterator& it);
  117. /**
  118. * Parses the current position as a variable name, extractig it's name and
  119. * increasing the iterator as the name is extracted
  120. *
  121. * @param it The position to start extracting the variable name from
  122. *
  123. * @return The variable name
  124. */
  125. std::string extractName (std::string::const_iterator& it);
  126. /**
  127. * Parses the current position as a quoted value, extracting it's value
  128. * and increasing the iterator at the same time
  129. *
  130. * @param it The position to start extracting the value from
  131. *
  132. * @return The value
  133. */
  134. std::string extractQuotedValue (std::string::const_iterator& it);
  135. /**
  136. * Tries to find the given shader file and compile it
  137. *
  138. * @param filename The shader's filename
  139. *
  140. * @return The compiled contents
  141. */
  142. std::string lookupShaderFile (std::string filename);
  143. /**
  144. * Searches for the given symbol in the replace table
  145. *
  146. * @param symbol The symbol to look for
  147. *
  148. * @return The symbol it should be replaced with
  149. */
  150. std::string lookupReplaceSymbol (std::string symbol);
  151. /**
  152. * @return Whether the character in the current position is a character or not
  153. */
  154. bool isChar (std::string::const_iterator& it);
  155. /**
  156. * @return Whether the character in the current position is a number or not
  157. */
  158. bool isNumeric (std::string::const_iterator& it);
  159. /**
  160. * The shader file this instance is loading
  161. */
  162. irr::io::path m_file;
  163. /**
  164. * The original file content
  165. */
  166. std::string m_content;
  167. /**
  168. * The final, compiled content ready to be used by OpenGL
  169. */
  170. std::string m_compiledContent;
  171. /**
  172. * Whether there was any kind of error in the compilation or not
  173. */
  174. bool m_error;
  175. /**
  176. * Extra information about the error (if any)
  177. */
  178. std::string m_errorInfo;
  179. /**
  180. * The type of shader
  181. */
  182. Type m_type;
  183. /**
  184. * The file resolver to be used by the compiler to load the files
  185. */
  186. wp::fs::fileResolver m_resolver;
  187. };
  188. }
  189. }
  190. #endif /* !__BASIC_SHADER_LOADER_H__ */