BasicShaderLoader.cpp 11 KB

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