compiler.cpp 14 KB

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