You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

shader_preprocess.cpp 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // We want glsl to support some preprocess directives:
  2. // just like: #ifdef...#endif, #include, #error, #warning, #pragma
  3. //
  4. // The following code snippet is an example.
  5. std::string Shader::PreprocessIncludes(const std::string& source, const boost::filesystem::path& filename, int level /*= 0 */)
  6. {
  7. PrintIndent();
  8. if(level > 32)
  9. LogAndThrow(ShaderException,"header inclusion depth limit reached, might be caused by cyclic header inclusion");
  10. using namespace std;
  11. static const boost::regex re("^[ ]*#[ ]*include[ ]+[\"<](.*)[\">].*");
  12. stringstream input;
  13. stringstream output;
  14. input << source;
  15. size_t line_number = 1;
  16. boost::smatch matches;
  17. string line;
  18. while(std::getline(input,line))
  19. {
  20. if (boost::regex_search(line, matches, re))
  21. {
  22. std::string include_file = matches[1];
  23. std::string include_string;
  24. try
  25. {
  26. include_string = Core::FileIO::LoadTextFile(include_file);
  27. }
  28. catch (Core::FileIO::FileNotFoundException& e)
  29. {
  30. stringstream str;
  31. str << filename.file_string() <<"(" << line_number << ") : fatal error: cannot open include file " << e.File();
  32. LogAndThrow(ShaderException,str.str())
  33. }
  34. output << PreprocessIncludes(include_string, include_file, level + 1) << endl;
  35. }
  36. else
  37. {
  38. output << "#line "<< line_number << " \"" << filename << "\"" << endl;
  39. output << line << endl;
  40. }
  41. ++line_number;
  42. }
  43. PrintUnindent();
  44. return output.str();
  45. }