BasicShaderLoader.cpp 11 KB

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