ProgramPrePreprocessor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/resource/ProgramPrePreprocessor.h>
  6. #include <anki/resource/ResourceManager.h>
  7. #include <anki/resource/ResourceFilesystem.h>
  8. #include <anki/util/Functions.h>
  9. #include <anki/util/File.h>
  10. #include <anki/util/Array.h>
  11. #include <iomanip>
  12. #include <cstring>
  13. namespace anki
  14. {
  15. //==============================================================================
  16. // Keep the strings in that order so that the start pragmas will match to the
  17. // ShaderType enums
  18. static Array<const char*, 7> commands = {{"#pragma anki type vert",
  19. "#pragma anki type tesc",
  20. "#pragma anki type tese",
  21. "#pragma anki type geom",
  22. "#pragma anki type frag",
  23. "#pragma anki type comp",
  24. "#pragma anki include \""}};
  25. const U32 MAX_DEPTH = 8;
  26. //==============================================================================
  27. void ProgramPrePreprocessor::printSourceLines() const
  28. {
  29. U i = 1;
  30. auto it = m_sourceLines.getBegin();
  31. auto end = m_sourceLines.getEnd();
  32. for(; it != end; ++it)
  33. {
  34. printf("%4d %s\n", int(i++), &(*it)[0]);
  35. }
  36. }
  37. //==============================================================================
  38. Error ProgramPrePreprocessor::parseFile(const ResourceFilename& filename)
  39. {
  40. // Parse files recursively
  41. Error err = parseFileForPragmas(filename, 0);
  42. if(!err)
  43. {
  44. m_sourceLines.join(m_alloc, "\n", m_shaderSource);
  45. }
  46. return err;
  47. }
  48. //==============================================================================
  49. Error ProgramPrePreprocessor::parseFileForPragmas(
  50. ResourceFilename filename, U32 depth)
  51. {
  52. // first check the depth
  53. if(depth > MAX_DEPTH)
  54. {
  55. ANKI_LOGE("The include depth is too high. "
  56. "Probably circular includance");
  57. return ErrorCode::USER_DATA;
  58. }
  59. // load file in lines
  60. StringAuto txt(m_alloc);
  61. StringListAuto lines(m_alloc);
  62. ResourceFilePtr file;
  63. ANKI_CHECK(m_manager->getFilesystem().openFile(filename, file));
  64. ANKI_CHECK(file->readAllText(TempResourceAllocator<char>(m_alloc), txt));
  65. lines.splitString(txt.toCString(), '\n');
  66. if(lines.getSize() < 1)
  67. {
  68. ANKI_LOGE("File is empty: %s", &filename[0]);
  69. return ErrorCode::USER_DATA;
  70. }
  71. for(const String& line : lines)
  72. {
  73. PtrSize npos = 0;
  74. if(line.find("#pragma anki") == 0)
  75. {
  76. Bool malformed = true;
  77. Bool found;
  78. ANKI_CHECK(parseType(line, found));
  79. if(found)
  80. {
  81. malformed = false; // All OK
  82. }
  83. else if((npos = line.find(commands[6])) == 0)
  84. {
  85. // Include
  86. if(line.getLength() >= std::strlen(commands[6]) + 2)
  87. {
  88. StringAuto filen(m_alloc);
  89. filen.create(line.begin() + std::strlen(commands[6]),
  90. line.end() - 1);
  91. StringAuto filenFixed(m_alloc);
  92. ANKI_CHECK(
  93. parseFileForPragmas(filen.toCString(), depth + 1));
  94. malformed = false; // All OK
  95. }
  96. }
  97. if(malformed)
  98. {
  99. ANKI_LOGE("Malformed pragma anki: %s", &line[0]);
  100. return ErrorCode::USER_DATA;
  101. }
  102. }
  103. else
  104. {
  105. m_sourceLines.pushBackSprintf(m_alloc, "%s", &line[0]);
  106. }
  107. }
  108. // Sanity checks
  109. if(m_type == ShaderType::COUNT)
  110. {
  111. ANKI_LOGE("Shader is missing the type");
  112. return ErrorCode::USER_DATA;
  113. }
  114. return ErrorCode::NONE;
  115. }
  116. //==============================================================================
  117. Error ProgramPrePreprocessor::parseType(const String& line, Bool& found)
  118. {
  119. U i;
  120. found = false;
  121. for(i = 0; i < static_cast<U>(ShaderType::COUNT); i++)
  122. {
  123. if(line.find(commands[i]) == 0)
  124. {
  125. found = true;
  126. break;
  127. }
  128. }
  129. if(found)
  130. {
  131. if(m_type != ShaderType::COUNT)
  132. {
  133. ANKI_LOGE("The shader type is already set. Line %s", &line[0]);
  134. return ErrorCode::USER_DATA;
  135. }
  136. else
  137. {
  138. m_type = static_cast<ShaderType>(i);
  139. }
  140. }
  141. return ErrorCode::NONE;
  142. }
  143. } // end namespace anki