ProgramPrePreprocessor.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "anki/gl/GlCommon.h"
  11. namespace anki {
  12. /// Helper class used for shader program loading
  13. ///
  14. /// The class fills some of the GLSL spec deficiencies. It adds the include
  15. /// preprocessor directive and the support to have all the shaders in the same
  16. /// file. The file that includes all the shaders is called
  17. /// PrePreprocessor-compatible.
  18. ///
  19. /// The preprocessor pragmas are:
  20. ///
  21. /// - #pragma anki type <vert | tesc | tese | geom | frag | comp>
  22. /// - #pragma anki include "<filename>"
  23. class ProgramPrePreprocessor
  24. {
  25. private:
  26. using PPPStringList = StringListBase<TempResourceAllocator<char>>;
  27. using PPPString = TempResourceString;
  28. public:
  29. /// It loads a file and parses it
  30. /// @param[in] filename The file to load
  31. ProgramPrePreprocessor(ResourceManager* manager)
  32. : m_shaderSource(manager->_getTempAllocator()),
  33. m_sourceLines(manager->_getTempAllocator()),
  34. m_manager(manager)
  35. {}
  36. ~ProgramPrePreprocessor()
  37. {}
  38. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  39. /// the output
  40. ///
  41. /// @param filename The file to parse
  42. ANKI_USE_RESULT Error parseFile(const CString& filename);
  43. const PPPString& getShaderSource() const
  44. {
  45. ANKI_ASSERT(!m_shaderSource.isEmpty());
  46. return m_shaderSource;
  47. }
  48. ShaderType getShaderType() const
  49. {
  50. ANKI_ASSERT(m_type != ShaderType::COUNT);
  51. return m_type;
  52. }
  53. protected:
  54. /// The final program source
  55. PPPString m_shaderSource;
  56. /// The parseFileForPragmas fills this
  57. PPPStringList m_sourceLines;
  58. /// Shader type
  59. ShaderType m_type = ShaderType::COUNT;
  60. /// Keep the manager for some path conversions.
  61. ResourceManager* m_manager;
  62. /// A recursive function that parses a file for pragmas and updates the
  63. /// output
  64. ///
  65. /// @param filename The file to parse
  66. /// @param depth The #line in GLSL does not support filename so an
  67. /// depth it being used. It also tracks the includance depth
  68. ANKI_USE_RESULT Error parseFileForPragmas(
  69. const PPPString& filename, U32 depth);
  70. /// Parse the type
  71. ANKI_USE_RESULT Error parseType(const PPPString& line, Bool& found);
  72. void printSourceLines() const; ///< For debugging
  73. };
  74. } // end namespace anki
  75. #endif