compiler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include <irrlicht/irrlicht.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <wallpaperengine/config.h>
  6. #include "compiler.h"
  7. #include "nier_test.h"
  8. #include "common.h"
  9. #include "../irrlicht.h"
  10. #include "../fs/fileResolver.h"
  11. namespace wp
  12. {
  13. namespace shaders
  14. {
  15. compiler::compiler (irr::io::path file, Type type, bool recursive)
  16. {
  17. // begin with an space so it gets ignored properly on parse
  18. if (recursive == false)
  19. {
  20. this->m_content = "#version 120\n"
  21. "#define texSample2D texture2D\n"
  22. "#define frac fract\n"
  23. "vec4 mul(vec4 x, mat4 y) { return x * y; }\n";
  24. }
  25. else
  26. {
  27. this->m_content = "";
  28. }
  29. std::ifstream _in (file.c_str ());
  30. this->m_content.append (std::istreambuf_iterator<char> (_in), std::istreambuf_iterator<char> ());
  31. this->m_type = type;
  32. this->m_resolver = wp::fs::resolver.clone ();
  33. this->m_resolver.appendEnvironment (wp::config::path::shaders);
  34. this->m_file = file;
  35. }
  36. bool compiler::peekString(std::string str, std::string::const_iterator& it)
  37. {
  38. std::string::const_iterator check = str.begin();
  39. std::string::const_iterator cur = it;
  40. while (cur != this->m_content.end () && check != str.end ())
  41. {
  42. if (*cur != *check) return false;
  43. cur ++; check ++;
  44. }
  45. if (cur == this->m_content.end ())
  46. {
  47. return false;
  48. }
  49. if (check != str.end ())
  50. {
  51. return false;
  52. }
  53. it = cur;
  54. return true;
  55. }
  56. bool compiler::expectSemicolon (std::string::const_iterator& it)
  57. {
  58. if (*it != ';')
  59. {
  60. this->m_error = true;
  61. this->m_errorInfo = "Expected semicolon but got " + *it;
  62. return false;
  63. }
  64. it ++;
  65. return true;
  66. }
  67. void compiler::ignoreSpaces(std::string::const_iterator &it)
  68. {
  69. while (it != this->m_content.end() && (*it == ' ' || *it == '\t')) it ++;
  70. }
  71. void compiler::ignoreUpToNextLineFeed (std::string::const_iterator& it)
  72. {
  73. while (it != this->m_content.end() && *it != '\n') it ++;
  74. }
  75. void compiler::ignoreUpToBlockCommentEnd (std::string::const_iterator& it)
  76. {
  77. while (it != this->m_content.end() && this->peekString ("*/", it) == false) it ++;
  78. }
  79. std::string compiler::extractType (std::string::const_iterator& it)
  80. {
  81. std::vector<std::string>::const_iterator cur = sTypes.begin ();
  82. std::vector<std::string>::const_iterator end = sTypes.end ();
  83. while (cur != end)
  84. {
  85. if (this->peekString (*cur, it) == true)
  86. {
  87. return *cur;
  88. }
  89. cur ++;
  90. }
  91. this->m_error = true;
  92. this->m_errorInfo = "Expected type";
  93. return "";
  94. }
  95. std::string compiler::extractName (std::string::const_iterator& it)
  96. {
  97. std::string::const_iterator cur = it;
  98. std::string::const_iterator begin = cur;
  99. // first character has to be a valid alphabetic characer
  100. if (this->isChar (cur) == false && *cur != '_')
  101. {
  102. this->m_error = true;
  103. this->m_errorInfo = "Expected name doesn't start with a valid character";
  104. return "";
  105. }
  106. cur ++;
  107. while (cur != this->m_content.end () && (this->isChar (cur) == true || *cur == '_' || this->isNumeric (cur) == true)) cur ++;
  108. it = cur;
  109. return std::string (begin, cur);
  110. }
  111. bool compiler::isChar (std::string::const_iterator& it)
  112. {
  113. return ((*it) >= 'A' && (*it) <= 'Z') || ((*it) >= 'a' && (*it) <= 'z');
  114. }
  115. bool compiler::isNumeric (std::string::const_iterator& it)
  116. {
  117. return (*it) >= '0' && (*it) <= '9';
  118. }
  119. std::string compiler::extractQuotedValue(std::string::const_iterator& it)
  120. {
  121. std::string::const_iterator cur = it;
  122. if (*cur != '"')
  123. {
  124. m_error = true;
  125. m_errorInfo = "Expected opening \" but got " + (*cur);
  126. return "";
  127. }
  128. cur ++;
  129. while (cur != this->m_content.end () && *cur != '\n' && *cur != '"') cur ++;
  130. if (cur == this->m_content.end ())
  131. {
  132. m_error = true;
  133. m_errorInfo = "Expected closing \" not found";
  134. it = cur;
  135. return "";
  136. }
  137. std::string filename = std::string (++it, cur);
  138. it = ++cur;
  139. return filename;
  140. }
  141. std::string compiler::lookupShaderFile (std::string filename)
  142. {
  143. irr::io::path shader = this->m_resolver.resolve (filename.c_str ());
  144. irr::io::IFileSystem *fs = wp::irrlicht::device->getFileSystem ();
  145. // by default try to load from current folder
  146. // we might want, in the future, to load things from different folders
  147. // to provide standard functionality
  148. if (fs->existFile (shader) == false)
  149. {
  150. this->m_error = true;
  151. this->m_errorInfo = "Cannot find file " + filename + " to include";
  152. return "";
  153. }
  154. // now compile the new shader
  155. // do not include the default header (as it's already included in the parent)
  156. compiler loader (shader, this->m_type, true);
  157. return loader.precompile ();
  158. }
  159. std::string compiler::lookupReplaceSymbol (std::string symbol)
  160. {
  161. std::map<std::string, std::string>::const_iterator cur = sVariableReplacement.begin ();
  162. std::map<std::string, std::string>::const_iterator end = sVariableReplacement.end ();
  163. while (cur != end)
  164. {
  165. if (cur->first == symbol)
  166. {
  167. return cur->second;
  168. }
  169. cur ++;
  170. }
  171. // if there is no replacement, return the original
  172. return symbol;
  173. }
  174. std::string compiler::precompile()
  175. {
  176. #define BREAK_IF_ERROR if (this->m_error == true) { wp::irrlicht::device->getLogger ()->log ("ERROR COMPILING SHADER"); wp::irrlicht::device->getLogger ()->log (this->m_errorInfo.c_str ()); return ""; }
  177. // parse the shader and find #includes and such things and translate them to the correct name
  178. // also remove any #version definition to prevent errors
  179. std::string::const_iterator it = this->m_content.begin ();
  180. // reset error indicator
  181. this->m_error = false;
  182. this->m_errorInfo = "";
  183. // search preprocessor macros and parse them
  184. while (it != this->m_content.end () && this->m_error == false)
  185. {
  186. if (*it == ' ' || *it == '\t' || *it == '\n' || *it == '\r' || *it == '\0' || *it == '{' || *it == '}' || *it == '[' || *it == ']' || *it == '.')
  187. {
  188. this->m_compiledContent += *it;
  189. it ++;
  190. }
  191. else if (*it == '#')
  192. {
  193. if (this->peekString ("#include", it) == true)
  194. {
  195. std::string filename = "";
  196. // ignore whitespaces
  197. this->ignoreSpaces (it); BREAK_IF_ERROR
  198. // extract value between quotes
  199. filename = this->extractQuotedValue (it); BREAK_IF_ERROR
  200. // try to find the file first
  201. this->m_compiledContent += "// begin of included from file " + filename + "\r\n";
  202. this->m_compiledContent += this->lookupShaderFile (filename);
  203. this->m_compiledContent += "\r\n// end of included from file " + filename + "\r\n";
  204. }
  205. else
  206. {
  207. this->m_compiledContent += '#';
  208. it ++;
  209. }
  210. }
  211. else if (*it == 'a')
  212. {
  213. // find attribute definitions
  214. if (this->peekString ("attribute", it) == true)
  215. {
  216. this->ignoreSpaces (it);
  217. std::string type = this->extractType (it); BREAK_IF_ERROR
  218. this->ignoreSpaces (it);
  219. std::string name = this->extractName (it); BREAK_IF_ERROR
  220. this->ignoreSpaces (it);
  221. this->expectSemicolon (it); BREAK_IF_ERROR
  222. this->m_compiledContent += "// attribute";
  223. this->m_compiledContent += " " + type + " ";
  224. this->m_compiledContent += name;
  225. this->m_compiledContent += "; /* replaced by " + this->lookupReplaceSymbol (name) + " */";
  226. }
  227. else
  228. {
  229. // check for types first
  230. std::string type = this->extractType (it);
  231. // types not found, try names
  232. if (this->m_error == false)
  233. {
  234. this->m_compiledContent += type;
  235. }
  236. else
  237. {
  238. this->m_error = false;
  239. std::string name = this->extractName (it);
  240. if (this->m_error == false)
  241. {
  242. // check if the name is a translated one or not
  243. this->m_compiledContent += this->lookupReplaceSymbol (name);
  244. }
  245. else
  246. {
  247. this->m_error = false;
  248. this->m_compiledContent += *it;
  249. it ++;
  250. }
  251. }
  252. }
  253. }
  254. else if (*it == '/')
  255. {
  256. if (this->peekString ("//", it) == true)
  257. {
  258. std::string::const_iterator begin = it - 2;
  259. this->ignoreUpToNextLineFeed (it);
  260. this->m_compiledContent.append (begin, it);
  261. }
  262. else if (this->peekString ("/*", it) == true)
  263. {
  264. std::string::const_iterator begin = it - 2;
  265. this->ignoreUpToBlockCommentEnd (it);
  266. this->m_compiledContent.append (begin, it);
  267. }
  268. else
  269. {
  270. this->m_compiledContent += *it;
  271. it ++;
  272. }
  273. }
  274. else
  275. {
  276. // check for types first
  277. std::string type = this->extractType (it);
  278. // types not found, try names
  279. if (this->m_error == false)
  280. {
  281. this->m_compiledContent += type;
  282. }
  283. else
  284. {
  285. this->m_error = false;
  286. std::string name = this->extractName (it);
  287. if (this->m_error == false)
  288. {
  289. // check if the name is a translated one or not
  290. this->m_compiledContent += this->lookupReplaceSymbol (name);
  291. }
  292. else
  293. {
  294. this->m_error = false;
  295. this->m_compiledContent += *it;
  296. it ++;
  297. }
  298. }
  299. }
  300. }
  301. wp::irrlicht::device->getLogger ()->log ("Compiled shader output for", this->m_file.c_str ());
  302. wp::irrlicht::device->getLogger ()->log (this->m_compiledContent.c_str ());
  303. return this->m_compiledContent;
  304. #undef BREAK_IF_ERROR
  305. }
  306. std::map<std::string, std::string> compiler::sVariableReplacement =
  307. {
  308. // attribute vec3 a_position
  309. {"a_Position", "gl_Vertex.xyz"},
  310. // attribute vec2 a_TexCoord
  311. {"a_TexCoord", "gl_MultiTexCoord0.xy"},
  312. // attribute vec3 a_Normal
  313. {"a_Normal", "gl_Normal.xyz"}
  314. };
  315. std::vector<std::string> compiler::sTypes =
  316. {
  317. "vec4", "vec3", "vec2", "float"
  318. };
  319. }
  320. }