compiler.cpp 14 KB

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