ShaderProgramResource.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "anki/resource/ShaderProgramResource.h"
  2. #include "anki/resource/ShaderProgramPrePreprocessor.h"
  3. #include "anki/core/App.h" // To get cache dir
  4. #include "anki/util/Util.h"
  5. #include "anki/util/Exception.h"
  6. #include <boost/filesystem.hpp>
  7. #include <fstream>
  8. #include <sstream>
  9. namespace anki {
  10. //==============================================================================
  11. void ShaderProgramResource::load(const char* filename)
  12. {
  13. ShaderProgramPrePreprocessor pars(filename);
  14. boost::array<const char*, 128> trfVarsArr = {{nullptr}};
  15. if(pars.getTranformFeedbackVaryings().size() > 0)
  16. {
  17. uint i;
  18. for(i = 0; i < pars.getTranformFeedbackVaryings().size(); i++)
  19. {
  20. trfVarsArr[i] = pars.getTranformFeedbackVaryings()[i].c_str();
  21. }
  22. trfVarsArr[i] = nullptr;
  23. }
  24. create(pars.getShaderSource(ST_VERTEX).c_str(),
  25. nullptr,
  26. nullptr,
  27. nullptr,
  28. pars.getShaderSource(ST_FRAGMENT).c_str(),
  29. &trfVarsArr[0]);
  30. }
  31. //==============================================================================
  32. std::string ShaderProgramResource::createSrcCodeToCache(
  33. const char* sProgFPathName, const char* preAppendedSrcCode)
  34. {
  35. using namespace boost::filesystem;
  36. if(strlen(preAppendedSrcCode) < 1)
  37. {
  38. return sProgFPathName;
  39. }
  40. // Create suffix
  41. boost::hash<std::string> stringHash;
  42. std::size_t h = stringHash(preAppendedSrcCode);
  43. std::string suffix = boost::lexical_cast<std::string>(h);
  44. //
  45. path newfPathName = AppSingleton::get().getCachePath()
  46. / (path(sProgFPathName).filename().string() + "." + suffix);
  47. if(exists(newfPathName))
  48. {
  49. return newfPathName.string();
  50. }
  51. std::string src_ = Util::readFile(sProgFPathName);
  52. std::string src = preAppendedSrcCode + src_;
  53. std::ofstream f(newfPathName.string().c_str());
  54. if(!f.is_open())
  55. {
  56. throw ANKI_EXCEPTION("Cannot open file for writing \""
  57. + newfPathName.string() + "\"");
  58. }
  59. f.write(src.c_str(), src.length());
  60. return newfPathName.string();
  61. }
  62. } // end namespace