ShaderProgramPrePreprocessor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef ANKI_RESOURCE_SHADER_PROGRAM_PRE_PREPROCESSOR_H
  2. #define ANKI_RESOURCE_SHADER_PROGRAM_PRE_PREPROCESSOR_H
  3. #include "anki/resource/ShaderProgramCommon.h"
  4. #include "anki/util/Vector.h"
  5. #include "anki/util/StdTypes.h"
  6. #include "anki/util/StringList.h"
  7. #include "anki/util/Array.h"
  8. #include <limits>
  9. namespace anki {
  10. /// Helper class used for shader program loading
  11. ///
  12. /// The class fills some of the GLSL spec deficiencies. It adds the include
  13. /// preprocessor directive and the support to have all the shaders in the same
  14. /// file. The file that includes all the shaders is called
  15. /// PrePreprocessor-compatible.
  16. ///
  17. /// The preprocessor pragmas are:
  18. ///
  19. /// - #pragma anki start <vertexShader | tcShader | teShader |
  20. /// geometryShader | fragmentShader>
  21. /// - #pragma anki include "<filename>"
  22. /// - #pragma anki transformFeedbackVaryings <separate|interleaved>
  23. /// <varName> <varName>
  24. ///
  25. /// @note The order of the "#pragma anki start" is important
  26. class ShaderProgramPrePreprocessor
  27. {
  28. public:
  29. enum XfbBufferMode
  30. {
  31. XFBBM_NONE,
  32. XFBBM_INTERLEAVED,
  33. XFBBM_SEPARATE
  34. };
  35. /// It loads a file and parses it
  36. /// @param[in] filename The file to load
  37. /// @exception Exception
  38. ShaderProgramPrePreprocessor(const char* filename)
  39. {
  40. parseFile(filename);
  41. }
  42. /// Destructor does nothing
  43. ~ShaderProgramPrePreprocessor()
  44. {}
  45. /// @name Accessors
  46. /// @{
  47. const StringList& getTranformFeedbackVaryings() const
  48. {
  49. return trffbVaryings;
  50. }
  51. const std::string& getShaderSource(ShaderType type)
  52. {
  53. return shaderSources[type];
  54. }
  55. XfbBufferMode getXfbBufferMode() const
  56. {
  57. return xfbBufferMode;
  58. }
  59. /// @}
  60. protected:
  61. /// The pragma base class
  62. struct Pragma
  63. {
  64. I32 definedLine = -1;
  65. Bool isDefined() const
  66. {
  67. return definedLine != -1;
  68. }
  69. };
  70. struct CodeBeginningPragma: Pragma
  71. {};
  72. /// Names and and ids for transform feedback varyings
  73. StringList trffbVaryings;
  74. XfbBufferMode xfbBufferMode = XFBBM_NONE;
  75. Array<std::string, ST_NUM> shaderSources;
  76. /// The parseFileForPragmas fills this
  77. StringList sourceLines;
  78. Array<CodeBeginningPragma, ST_NUM> shaderStarts;
  79. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  80. /// the output
  81. ///
  82. /// @param filename The file to parse
  83. /// @exception Ecxeption
  84. void parseFile(const char* filename);
  85. /// Called by parseFile
  86. void parseFileInternal(const char* filename);
  87. /// A recursive function that parses a file for pragmas and updates the
  88. /// output
  89. ///
  90. /// @param filename The file to parse
  91. /// @param depth The #line in GLSL does not support filename so an
  92. /// depth it being used. It also tracks the includance depth
  93. /// @exception Exception
  94. void parseFileForPragmas(const std::string& filename, U32 depth = 0);
  95. /// Self explanatory
  96. void parseStartPragma(U32 shaderType, const std::string& line);
  97. void printSourceLines() const; ///< For debugging
  98. };
  99. } // end namespace anki
  100. #endif