ProgramPrePreprocessor.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2014, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_RESOURCE_PROGRAM_PRE_PREPROCESSOR_H
  6. #define ANKI_RESOURCE_PROGRAM_PRE_PREPROCESSOR_H
  7. #include "anki/resource/ResourceManager.h"
  8. #include "anki/util/StdTypes.h"
  9. #include "anki/util/StringList.h"
  10. namespace anki {
  11. /// Shader type
  12. enum class ShaderType
  13. {
  14. VERTEX,
  15. TC,
  16. TE,
  17. GEOMETRY,
  18. FRAGMENT,
  19. COMPUTE,
  20. COUNT
  21. };
  22. /// Helper class used for shader program loading
  23. ///
  24. /// The class fills some of the GLSL spec deficiencies. It adds the include
  25. /// preprocessor directive and the support to have all the shaders in the same
  26. /// file. The file that includes all the shaders is called
  27. /// PrePreprocessor-compatible.
  28. ///
  29. /// The preprocessor pragmas are:
  30. ///
  31. /// - #pragma anki type <vert | tesc | tese | geom | frag | comp>
  32. /// - #pragma anki include "<filename>"
  33. class ProgramPrePreprocessor
  34. {
  35. private:
  36. using PPPStringList = StringListBase<TempResourceAllocator<char>>;
  37. using PPPString = TempResourceString;
  38. public:
  39. /// It loads a file and parses it
  40. /// @param[in] filename The file to load
  41. ProgramPrePreprocessor(
  42. const CString& filename, ResourceManager* manager)
  43. : m_shaderSource(manager->_getTempAllocator()),
  44. m_sourceLines(manager->_getTempAllocator()),
  45. m_manager(manager)
  46. {
  47. parseFile(filename);
  48. }
  49. ~ProgramPrePreprocessor()
  50. {}
  51. /// @name Accessors
  52. /// @{
  53. const PPPString& getShaderSource() const
  54. {
  55. ANKI_ASSERT(!m_shaderSource.isEmpty());
  56. return m_shaderSource;
  57. }
  58. ShaderType getShaderType() const
  59. {
  60. ANKI_ASSERT(m_type != ShaderType::COUNT);
  61. return m_type;
  62. }
  63. /// @}
  64. protected:
  65. /// The final program source
  66. PPPString m_shaderSource;
  67. /// The parseFileForPragmas fills this
  68. PPPStringList m_sourceLines;
  69. /// Shader type
  70. ShaderType m_type = ShaderType::COUNT;
  71. /// Keep the manager for some path conversions.
  72. ResourceManager* m_manager;
  73. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  74. /// the output
  75. ///
  76. /// @param filename The file to parse
  77. void parseFile(const CString& filename);
  78. /// A recursive function that parses a file for pragmas and updates the
  79. /// output
  80. ///
  81. /// @param filename The file to parse
  82. /// @param depth The #line in GLSL does not support filename so an
  83. /// depth it being used. It also tracks the includance depth
  84. void parseFileForPragmas(const PPPString& filename, U32 depth);
  85. /// Parse the type
  86. Bool parseType(const PPPString& line);
  87. void printSourceLines() const; ///< For debugging
  88. };
  89. } // end namespace anki
  90. #endif