compiler.h 7.2 KB

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