compiler.cpp 14 KB

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